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

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by hamid shakeri on 21 Aug 2014

Hi I'm trying to execute a program but I keep getting this error:

"Attempted to access indx(1); index out of bounds because numel(indx)=0"

I understand what it means, but I'm not able to fixe it, I need some help.

Here is the code with the error:

[nel,dum]=size(Edof); ned=dum-1; [n,nsd]=size(Coord); [n,nd]=size(Dof); nend=ned/nen; % for i = 1:nel nodnum=zeros(1,nen); for j = 1:nen check=Dof(:,1:nend)-ones(n,1)*Edof(i,(j-1)*nend+2:j*nend+1); [indx,dum]=find(check==0); nodnum(j)=indx(1); end % Ex(i,:)=Coord(nodnum,1)'; if nsd>1 Ey(i,:)=Coord(nodnum,2)'; end if nsd>2 Ez(i,:)=Coord(nodnum,3)'; end end

Thank you guys

Answer by Andrew Reibold on 21 Aug 2014

Edited by Andrew Reibold on 21 Aug 2014

My assessment: It found no values where check was equal to zero, so there was no value for 'indx'.

I don't have everything I need to double check this, but make sure that

[indx,dum]=find(check==0);

really is returning a result.

Answer by Geoff Hayes on 21 Aug 2014

Hamid - What is the data type of the numbers in **Dof** and **Edof** - integers or floats? If floats, then you will not be able to use **find(check==0)** and will have to use some sort of tolerance around the numbers to see which are close enough to zero i.e.

check = [1 1.2 0.00000003 3.2 -0.00000004 4 12 55]'; tol = 0.000001; [rowIdcs,colIdcs]=find(abs(check)<tol);

And then, check to see if anything is returned before accessing **rowIdcs**

if ~iesmpty(rowIdcs) nodnum(j)= rowIdcs(1); end

(I renamed **indx** and **dum** to **rowIdcs** and **colIdcs** because that is what the two output parameters refer to - the row indices and the column indices of the elements of **check** that satisfy the criteria.)

In the above example, since **check** is a column vector (due to the transpose '), then **rowIdcs** will have the indices of the rows of **check** that satisfy the criteria

rowIdcs =

3 5

and those values are

check(rowIdcs)

ans =

1.0e-07 *

0.3000 -0.4000

But you need to be careful. If **check** happens to be a row vector, then you need to use **colIdcs** to access those elements in **check**. And...if **check** happens to be a matrix, then you will need to use both vectors, **rowIdcs** and **colIdcs**, to get the correct value from **check**.

Show 1 older comment

Geoff Hayes on 21 Aug 2014

Hamid - can you attach (using the paperclip button) the complete code for this script? I don't know why you are getting this error especially as **indx** appears to be a 2x1 matrix, so **indx(1)** should work.

Geoff Hayes on 21 Aug 2014

Hamid - I wasn't asking for your complete GA package! I had thought that the code from above was embedded within a script or a function, and not a function itself that is part of a larger program.

I do not have the **Global Optimization Toolbox** so cannot run this code. I also don't know what your choice of inputs are when you start up the **GAmodule**.

As for the images that you attached, were they taken from when the error was being generated or just on the first call to this function? I'm asking because I noticed that you had a breakpoint in the code which implies that the code stopped here because of the breakpoint and not because of the **dbstop if error**. Did you try it with this statement?

Looking more closely at the code, it could be possible that the inputs result in an matrix that is empty during the calculation for **check**, especially as the **nend** is computed as

nend=ned/nen;

This could be a rational number, and may cause a problem with **Dof(:,1:nend)** and the other instances of its use in the function.

This is what I would like you to do: execute the statement **dbstop if error** prior to running your code. Then run **GAmodule** and wait for the debugger to stop at the line that fails. Then describe here what each of the inputs are to the **coordxtr** function

Edof Coord Dof nen

Either paste the values for each to a comment, or save these inputs to a mat file and attach it to a comment.

Geoff Hayes on 22 Aug 2014

Hamid - the images that you posted were **not** for when the error was generated but for the first iteration (when **i=1** and **j=1**) when everything worked fine. The code fails on the next iteration (when **i=1** but **j=2**) because

K>> check

check = -2 -2 1 1 4 4 7 7

Notice that **check** does not have any zeros. So the line of code

[indx,dum]=find(check==0);

will mean that

K>> indx

indx = Empty matrix: 0-by-1

K>> dum

dum = Empty matrix: 0-by-1

And so

nodnum(j)=indx(1);

generates the

Attempted to access indx(1); index out of bounds because numel(indx)=0

error. You will have to modify this code to handle the event where there are no zeros in the **check** matrix. For example,

if ~isempty(indx) nodnum(j)=indx(1); end

So please add these three lines of code and try again.

Note that you may still have a problem if **nodum** is never updated because **check** is always empty, and so the output **Ex**, **Ey**, and **Ez** may be invalid.

## 0 Comments