Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6763677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:30:18+00:00 2026-05-26T14:30:18+00:00

I am trying to achieve a very simple thing in MATLAB. I have a

  • 0

I am trying to achieve a very simple thing in MATLAB. I have a sequence of images in a 4D array in the following format: (framenumber, height, width, RGB).

My stack is quite small, it’s size is: 99 480 640 3

What I am trying to achieve is to detect if there was a change in motion between n consequtive images (the images taken with a fixed camera about moving objects).

For this, my most obvious idea is to subtract different images in the stack, and keep them in an other stack.

Here is the very simple code I tried to run:

vec = 1:size(stack,1)-5
dif = uint8(squeeze(abs(stack(vec,:,:,:) - stack(vec+5,:,:,:))));

However it gives me out of memory error.

Error using  - 
Out of memory. Type HELP MEMORY for your options.

If I don’t use squeeze (do I need to use squeeze here?) I even get a windows system error about memory being low.

As a fallback I wrote a for loop, which seems to work, but only if I use an uint8 cast. If not, it runs out of memory and gives windows system error.

for i = 1:size(stack,1)-5   
    dif(i,:,:,:) = uint8(abs(stack(i,:,:,:) - stack(i+5,:,:,:)));
end

Also, it takes 20 seconds to process this not so big dataset (only 99 frames).

Can you tell me how should I write a function which doesn’t try to keep everything in memory? This command should only require a very little memory for the 2 images being compared, however MATLAB tries to do all the operations in memory for some reason. The whole dataset should be 91 MB in memory so it clearly shouldn’t be a problem.

Also, can you explain if I need to use squeeze or not? When I try to get a single image, then

dif = stack(1,:,:,:) - stack(5,:,:,:);

produces a 4D array, while

dif = squeeze(stack(1,:,:,:) - stack(5,:,:,:));

produces an image format. But in the for loop above, it produces the same results both with and without squeeze. Why is this happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:30:18+00:00Added an answer on May 26, 2026 at 2:30 pm

    Regarding SQUEEZE, stack is a 4D matrix, so dif = stack(1,:,:,:) - stack(5,:,:,:); is also a 4D matrix, but the first dimension is singleton. So when you assign the result into an existing matrix (with indexing), the elements gets filled in order, and any singleton dimensions have no effect. However, putting the result into a new variable, the shape of the 4D matrix is maintained…

    You should be aware of your data types, if all values are integers in the [0,255] range, then cast the whole stack of images as UINT8 to begin with. As I mentioned in the comments, a 4D double matrix of such dimensions would require around 700MB of contiguous memory.

    Now in the for-loop, you should preallocate the dif matrix:

    dif = zeros([99-5 480 640 3],'uint8');
    for i = 1:size(stack,1)-5   
        dif(i,:,:,:) = abs( stack(i,:,:,:) - stack(i+5,:,:,:) );
    end
    

    Finally eventhough vectorized calls could be faster, they do require more memory as they try to allocate the entire space needed at one (as opposed to using a loop which only needs space to hold one single image at-a-time)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to achieve a very simple thing using C++ templates. I would
I am trying to achieve a very simple goal, however it does not seem
I have been trying very hard to achieve rounded corners with IE6+jquery ui tabs.
I am trying to do a very simple thing. I want that when I
I have some very simple code which works perfect for what look I'm trying
I'm trying to achieve a very basic goal that used to be quite simple
What I'm trying to achieve is very simple, but a bit difficult to explain:
im trying to achieve something but i dont really know how I have set
I have a very simple form for products. Each product has a checkbox and
This is probably a very simple one to be answered... I have a piece

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.