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

Thread Subject:
index out of bounds error

Subject: index out of bounds error

From: nur

Date: 1 Mar, 2013 04:34:08

Message: 1 of 8

 Hi,
I'm doing a research on crowd behavior analysis.I've a problem with the coding below;

 clear; clc; close all;
mx=45; my=45;
X=10;
d=zeros(mx,my); dx=zeros(mx,my); dy=zeros(mx,my);

Lx=10;Ly=10;
hx=Lx/(mx-1); hy=Ly/(my-1);
x=0:Lx/(mx-1):Lx; y=0:Ly/(my-1):Ly;
 
N_Obst=2; xx=[X]; yy=[X]; rr=[X];
 
d(1:mx, 1:my)=Lx+Ly;
for i=1:1:mx
    x(i)=(i-1)*hx;
    for j=1:1:my
        y(j)=(j-1)*hy;
 
        for n=1:1:N_Obst
            dtemp=sqrt((xx(n)-x(i))^2+(yy(n)-y(j))^2)-rr(n);
              % dtemp=sqrt((x(n)-x(i))^2+(y(n)-y(j))^2);
            if dtemp<d(i,j); d(i,j)=dtemp; end
        end

Error => Attempted to access xx(2); index out of bounds because numel(xx)=1.

Anyone can help me solve this problem?
Thanks in advance.

Subject: index out of bounds error

From: Nasser M. Abbasi

Date: 1 Mar, 2013 04:42:58

Message: 2 of 8

On 2/28/2013 10:34 PM, nur wrote:
> Hi,
> I'm doing a research on crowd behavior analysis.I've a problem with the coding below;
>
> clear; clc; close all;
> mx=45; my=45;
> X=10;
> d=zeros(mx,my); dx=zeros(mx,my); dy=zeros(mx,my);
>
> Lx=10;Ly=10;
> hx=Lx/(mx-1); hy=Ly/(my-1);
> x=0:Lx/(mx-1):Lx; y=0:Ly/(my-1):Ly;
>
> N_Obst=2; xx=[X]; yy=[X]; rr=[X];
>
> d(1:mx, 1:my)=Lx+Ly;
> for i=1:1:mx
> x(i)=(i-1)*hx;
> for j=1:1:my
> y(j)=(j-1)*hy;
>
> for n=1:1:N_Obst
> dtemp=sqrt((xx(n)-x(i))^2+(yy(n)-y(j))^2)-rr(n);
> % dtemp=sqrt((x(n)-x(i))^2+(y(n)-y(j))^2);
> if dtemp<d(i,j); d(i,j)=dtemp; end
> end
>
> Error => Attempted to access xx(2); index out of bounds because numel(xx)=1.
>
> Anyone can help me solve this problem?
> Thanks in advance.
>

The code says at the top

xx=[X]

but what is X ??

Subject: index out of bounds error

From: nur

Date: 1 Mar, 2013 04:56:07

Message: 3 of 8


> The code says at the top
>
> xx=[X]
>
> but what is X ??
>
>
For ur info, X is the number of particles (which represents number of people).

Subject: index out of bounds error

From: Nasser M. Abbasi

Date: 1 Mar, 2013 05:15:56

Message: 4 of 8

On 2/28/2013 10:56 PM, nur wrote:
>
>> The code says at the top
>>
>> xx=[X]
>>
>> but what is X ??
>>
>>
> For ur info, X is the number of particles (which represents number of people).
>

Ok, but when you write

xx=[X] and X is a number, then this does not make an array.

It will just make it a number (whatever X value is).

To make xx an zero filled vector of length X do

xx=zeros(X,1)

that is why you getting the error

Error => Attempted to access xx(2); index out of bounds because numel(xx)=1.

Subject: index out of bounds error

From: nur

Date: 1 Mar, 2013 06:21:09

Message: 5 of 8

It's work.Thanks sir.really appreciate it.But here I have another same problem.
Could u help me?

clear; clc; close all;
mx=45; my=45;
X=10;
d=zeros(mx,my); dx=zeros(mx,my); dy=zeros(mx,my);

Lx=10;Ly=10;
hx=Lx/(mx-1); hy=Ly/(my-1);
x=0:Lx/(mx-1):Lx; y=0:Ly/(my-1):Ly;
 
N_Obst=2;
%xx=X;
xx=zeros(X,1);
yy=zeros(X,1);
rr=zeros(X,1);
 
d(1:mx, 1:my)=Lx+Ly;
for i=1:1:mx
    x(i)=(i-1)*hx;
    for j=1:1:my
        y(j)=(j-1)*hy;
 
        for n=1:1:N_Obst
            dtemp=sqrt((xx(n)-x(i))^2+(yy(n)-y(j))^2)-rr(n);
              % dtemp=sqrt((x(n)-x(i))^2+(y(n)-y(j))^2);
            if dtemp<d(i,j); d(i,j)=dtemp; end
        end
 
        dtemp=sqrt((x(i)-0.5*Lx)^2+(y(j)-0.45*Ly)^2);if dtemp<d(i,j); d(i,j)=dtemp; end
        dtemp=sqrt((x(i)-0.5*Lx)^2+(y(j)-0.55*Ly)^2);if dtemp<d(i,j); d(i,j)=dtemp; end
        if(y(j) <0.45*Ly), dtemp=sqrt((0.5*Lx-x(i))^2); if dtemp<d(i,j); d(i,j)=dtemp; end
        elseif (y(j) > 0.55*Ly), dtemp=sqrt((0.5*Lx-x(i))^2); if dtemp<d(i,j); d(i,j)=dtemp; end
        end
 
        dtemp=sqrt((Ly-y(j))^2); if dtemp<d(i,j); d(i,j)=dtemp; end
        dtemp=sqrt((0-y(j))^2); if dtemp<d(i,j); d(i,j)=dtemp; end
        dtemp=sqrt((0-x(i))^2); if dtemp<d(i,j); d(i,j)=dtemp; end
    end
end
 
 
for i=2:1:mx-1
    for j=2:1:my-1
        dx(i,j)=(d(i+1,j)-d(i-1,j))/(2*hx);
        dy(i,j)=(d(i,j+1)-d(i,j-1))/(2*hy);
    end
end
 
k=1.2*10^5; kap=2.4*10^5;
Ai=2*10^3; Bi=0.08;
mass=70; tau=0.5;
dt=0.0025;
endtime=15;
n=X;
 
up=zeros(1,n); vp=zeros(1,n);ugiven=zeros(1,n); vgiven=zeros(1,n);
xp=zeros(1,n); yp=zeros(1,n);
 
xp=[X];
yp=[X];
up=0*[X];
vp=[X];
ugiven=0*[X];
vgiven=[X];
rp(1:n)=0.3;
 
t=0:dt:endtime;
 
time(1)=0.0;esc(1)=0;
 
for p=1:length(t);
 
    for l=1:n;
        xold=xp(l); yold=yp(l); uold=up(l); vold=vp(l);
        fx=0; fy=0;
 
        ip=floor((xp(l)/Lx)*mx)+1; jp=floor((yp(l)/Ly)*my)+1;
        ALx=(xp(l)-hx*(ip-1))/hx; ARx=1.0-ALx;
        ALy=(yp(l)-hy*(jp-1))/hy; ARy=1.0-ALy;
        A1=ALx*ALy; A2=ARx*ALy; A3=ARx*ARy; A4=ALx*ARy;
 
        dwx=A1*(dx(ip+1,jp+1))+A2*(dx(ip,jp+1))+A3*(dx(ip,jp))+A4*(dx(ip+1,jp));
        dwy=A1*(dy(ip+1,jp+1))+A2*(dy(ip,jp+1))+A3*(dy(ip,jp))+A4*(dy(ip+1,jp));
        dw=A1*(d(ip+1,jp+1))+A2*(d(ip,jp+1))+A3*(d(ip,jp))+A4*(d(ip+1,jp));
 
        for m=1:n;
            if l~=m;
                dxp=xp(l)-xp(m); dyp=yp(l)-yp(m);
                dup=up(m)-up(l); dvp=vp(m)-vp(l);
                dij=sqrt(dxp^2+dyp^2);
                nx=dxp/dij; ny=dyp/dij;
                tx=-ny; ty=nx;
                tangVel=dup*tx+dvp*ty;
 
                g=0.0; if (rp(l)+rp(m)>dij); g=1.0;end;
                fx=fx+(Ai*exp((rp(l)+rp(m)-dij)/Bi)+k*g*(rp(l)+rp(m)-dij))*nx+...
                    kap*g*(rp(l)+rp(m)-dij)*tangVel*tx;
                fy=fy+(Ai*exp((rp(l)+rp(m)-dij)/Bi)+k*g*(rp(l)+rp(m)-dij))*ny+...
                    kap*g*(rp(l)+rp(m)-dij)*tangVel*ty;
            end
        end
 

returns error => Attempted to access dx(47,47); index out of bounds because size(dx)=[45,45].

Error in this line;
        dwx=A1*(dx(ip+1,jp+1))+A2*(dx(ip,jp+1))+A3*(dx(ip,jp))+A4*(dx(ip+1,jp));
 

Subject: index out of bounds error

From: Nasser M. Abbasi

Date: 1 Mar, 2013 06:41:40

Message: 6 of 8

On 3/1/2013 12:21 AM, nur wrote:
> It's work.Thanks sir.really appreciate it.But here I have another same problem.
> Could u help me?
>

You still have the same problem in different parts of the code:

> %xx=X;
> xx=zeros(X,1);

Ok, you fixed the above, but what about this, just look down:

>
> xp=[X];
...
> for p=1:length(t);
>
> for l=1:n;
> xold=xp(l); yold=yp(l); uold=up(l); vold=vp(l);


Also, do not use l for a variable, hard to read as it looks like a 1 on
the screen.

Subject: index out of bounds error

From: nur

Date: 8 Mar, 2013 04:46:06

Message: 7 of 8

"Nasser M. Abbasi" wrote in message <kgpij1$eb6$1@speranza.aioe.org>...
> On 3/1/2013 12:21 AM, nur wrote:
> > It's work.Thanks sir.really appreciate it.But here I have another same problem.
> > Could u help me?
> >
>
> You still have the same problem in different parts of the code:
>
> > %xx=X;
> > xx=zeros(X,1);
>
> Ok, you fixed the above, but what about this, just look down:
>
> >
> > xp=[X];
> ...
> > for p=1:length(t);
> >
> > for l=1:n;
> > xold=xp(l); yold=yp(l); uold=up(l); vold=vp(l);
>

Sorry sir.I did'nt get what u meant the error with for p=1:length(t);
Is there any wrong with it?
>
> Also, do not use l for a variable, hard to read as it looks like a 1 on
> the screen.
>

Subject: index out of bounds error

From: Nasser M. Abbasi

Date: 8 Mar, 2013 06:13:40

Message: 8 of 8

On 3/7/2013 10:46 PM, nur wrote:

>> ...
>>> for p=1:length(t);
>>>
>>> for l=1:n;
>>> xold=xp(l); yold=yp(l); uold=up(l); vold=vp(l);
>
> Sorry sir.I did'nt get what u meant the error with for p=1:length(t);
> Is there any wrong with it?

>>
>> Also, do not use l for a variable, hard to read as it looks like a 1 on
>> the screen.
>>

Are you are asking about the `1` vs `l` comment I made? if so, I meant
that it is not good idea to use l for variable name as it looks like 1
on the screen and can be confusing. I suggest using some other letter
for variable. like L or anything else, but not l

--Nasser

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us