MATLAB Answers


Maximum variable size allowed by the program is exceeded.

Asked by Algorithms Analyst on 4 Jan 2013
Accepted Answer by Matt J

Hi all I am applying a alogorithm after computing the gradient I am getting an error.My code is

if K>1

    [gradscale gidx]=max(maxgrad,[],3);
    for kn=1:K
        [rowidx colidx ]=ind2sub(size(gidx),find(gidx==kn));


??? Maximum variable size allowed by the program is exceeded.

Error in ==> Untitled at 36 gxtemp(rowidx,colidx)=gradscalex(rowidx,colidx,kn); Help me plz to solve this problem.I searched out but no solution.I am using Matlab version 7.11.0(R2010b) with 32-Bit operating system and 4GB RAM.


Do gradscalex, gradscaley, K, have values?

I think it would improve your chances to get an answer if you supplied reasonable values for gradscalex, gradscaley, and K. From the code I assume K is a scalar.

You have said rowidx and colidx are column vectors with 154723 elements each which I would guess means that approximately two-thirds of your maximum values occurred in the first third-dimensional layer, that is when kn = 1.

Are you aware that when you write


with kn = 1 this would produce an array of size 154723 by 154723? I am sure this is not what you intended and I am equally certain your machine is not going to let you have an array of that monstrous size.

You will have to revise your code to do what you really want at this point. If gradscalex and gradscaley are both M by N size, you probably should be using the output of find(gidx==kn) directly without using ind2sub in transferring the selected elements of gradscalex and gradscaley into gxtemp and gytemp, as seems to be your intention.


No products are associated with this question.

2 Answers

Answer by Matt J
on 5 Jan 2013
Edited by Matt J
on 5 Jan 2013
 Accepted answer

I have the vague impression that you're trying to do this:

     [mm,nn]=ndgrid(1:M,1:N);  %pre-compute once!!
     [~, gidx]=max(maxgrad,[],3);
     gxtemp=reshape( gradscalex(idx)  ,[M,N]);
     gytemp=reshape( gradscaley(idx)  ,[M,N]);

  1 Comment

Matt J
on 6 Jan 2013

Are we still talking about your originally posted code? If not, you should post a new question. However, the only likely place to get answers about a File Exchange file is from its author. That shouldn't be too hard. I notice he is rather actively responding to comments posted about the file. You could also try other HOG implementations on the File Exchange, e.g.,

If the code you posted, however, is one step in your own HOG code, it looks like that step is an edge detection step. You are trying to find some surface of maximum gradient in 3D and collect the gradient values along that 3D surface, storing them to gxtemp and gytemp. If I'm correct, the code I showed you should work fine. If not, you should explain better what the output, gxtemp and gytemp, are supposed to represent.

Answer by the cyclist
on 4 Jan 2013

Probably the simplest way to debug this is to set a breakpoint with

>> dbstop if error

before you run the code. Then, run your code. It will halt execution when it hits the error, and bring you to the editor automatically.

My guess is that you will find that one (or both) of the variables "rowidx" or "colidx" are much, much larger than you expect, and the offending line is therefore attempting to create a huge array.

After you are done debugging, you might want to clear the breakpoint with

>> dbclear if error


Jan Simon
on 5 Jan 2013

And what is kn? Beside the size, you need to know the maximum value also, when these are no logical indexes.

To Algorithms Analyst: Have you read my comment above made 13 hours ago? The line where the error occurs is trying to generate a 154723 by 154723 array! That is undoubtedly the cause of the error message and it is also surely not what you wanted to do.

To Algorithms Analyst: I recommend you try the following as an experiment. B begins here as a 3 x 3 array of zeros and we attempt to copy all the elements of the 3 x 3 array A into B except its center element.

 A = [10 20 30;40 50 60;70 80 90];
 B = zeros(size(A));
 rc = [1 1;2 1;3 1;3 2;3 3;2 3;1 3;1 2]; % All index pairs except center
 r = rc(:,1); % The row indices
 c = rc(:,2); % The corresponding column indices
 B(r,c) = A(r,c); % <-- The wrong way to copy them

As you see, it erroneously copied all nine elements. Looking at the intermediate result will begin to show why:

 T = A(r,c);

T is not a 3 x 3 matrix but is 8 x 8 which means that not only did the center element of A get copied but each element was repeatedly copied many times for a total of 64 copyings. If instead you do this:

 ix = sub2ind(size(A),r,c);
 B(ix) = A(ix)

then it works correctly and there are no repetitions, just eight copyings.

This means that you should have been using a linear index in the line in question, which in fact you had already computed with the 'find' operation. This also means that the intermediate result of gradscalex(rowidx,colidx,kn) would have had a monstrous 23,939,206,729 elements in it, a bit much for any one variable to possess, I think you'll agree. I don't blame matlab for complaining - the poor thing was suffering from memory loss.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!