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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Algorithms Analyst
on 4 Jan 2013

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

if K>1

maxgrad=sqrt(double(gradscalex.*gradscalex+gradscaley.*gradscaley)); [gradscale gidx]=max(maxgrad,[],3); gxtemp=zeros(M,N); gytemp=gxtemp; for kn=1:K [rowidx colidx ]=ind2sub(size(gidx),find(gidx==kn)); gxtemp(rowidx,colidx)=gradscalex(rowidx,colidx,kn); gytemp(rowidx,colidx)=gradscaley(rowidx,colidx,kn); end gradscalex=gxtemp; gradscaley=gytemp;

end

??? 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.

*No products are associated with this question.*

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:

[M,N,P]=size(gradscalex); [mm,nn]=ndgrid(1:M,1:N); %pre-compute once!!

maxgrad=sqrt(double(gradscalex.*gradscalex+gradscaley.*gradscaley)); [~, gidx]=max(maxgrad,[],3); idx=sub2ind([M,N,P],mm(:),nn(:),gidx(:));

gxtemp=reshape( gradscalex(idx) ,[M,N]); gytemp=reshape( gradscaley(idx) ,[M,N]);

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.,

http://www.mathworks.com/matlabcentral/fileexchange/33863

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

Show 3 older comments

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.

Roger Stafford
on 5 Jan 2013

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.

Roger Stafford
on 5 Jan 2013

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 B

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

T = A(r,c); T

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.

## 6 Comments

## Algorithms Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57955#comment_120630

the actual size of M and N is 512x512.

## Algorithms Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57955#comment_120631

one finall thing is size of rowidx is 154723 1.which is same as colidx..

## per isakson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57955#comment_120633

Did you try to analyze the behavior with the debugging features?

## per isakson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57955#comment_120635

Do gradscalex, gradscaley, K, have values?

## per isakson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57955#comment_120721

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.

## Roger Stafford (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/57955#comment_120770

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.