Index in position 1 exceeds array bounds (must not exceed 1).

Hello,
I have the Brownian motion model and I added In the plot a circle with radus R and center (X,Y). However I have two for loops for x and y to calcualte the model and I want to delete some points if they staisfies this condition:
(x(i)-X)^2 +(y(i)-Y)^2<r^2
When I run the code always gave this massage (Index in position 1 exceeds array bounds (must not exceed 1).) Sometimes is change the number such as ( ndex in position 20 exceeds array bounds (must not exceed 29).)
How I let inside the circle empty.
That what I wrote
please anyone help me for my problem with explain how I can solve this problems if I have simoilar in the future.
T=100;
Np=10000;
DX=20;
%Circle --------------------
A=10;
B=10;
R=30;
th=0:pi/100:2*pi;
X=R*cos(th)+A;
Y=R*sin(th)+B;
%Models
for j=1:m
for i=1:T
x(i+1,j)=x(i,j)+DX*randn();
y(i+1,j)=y(i,j)+DX*randn();
% Condition--------
COND= (x(i+1,j)-X).^2+(y(i+1,j)-Y).^2;
CONDD=int16(trap);
if trap <r^2
x(i+1,j)=[];
y(i+1,j)=[];
end
end
end

7 Comments

Hello,
Several variables are missing in your code, for example m, x, y, trap. Can you show us the code that we can understand welll?
Thank you so much for you try to help me
this is my code:
T=100;
m=10;
A=10;
B=10;
R=30;
th=0:pi/100:2*pi;
X=R*cos(th)+A;
Y=R*sin(th)+B;
x = zeros(1,m);
y = zeros(1,m);
DX=20;
for j=1:m %Number of particle
for i=1:T %Number of steps
x(i+1,j)=x(i,j)+DX*randn();
y(i+1,j)=y(i,j)+DX*randn();
COND= (x(i+1,j)-X).^2+(y(i+1,j)-Y).^2;
CONDD=int16(COND);
CONDD<=R^2;
x(1,:)=[];
y(1,:)=[];
end
end
plot(x,y,'k.');
hold on;
plot(X,Y,'r');
title (['Time = ', num2str(T),', Particles = ', num2str(m)]);
xlabel("space x");
ylabel("space y");
hold off;
Hello,
It is clearly that you declare "x = zeros(1,m)" i.e, x is vector having 1 row and m column, but in the loop for, you try to acess x(i+1,j) with i = 1:T. Therefore, if i = 1, then i + 1 = 2 and you will not able to acess the element x(2,j) since x has only 1 row.
Thank you so much for your answer.
However if I cancell the condition the code is working but when I but the condition gave me this massage:
Index in position 1 exceeds array bounds (must not exceed 16).
for different value of m or t
please help me to fix this code
Hello,
What do you mean "cancell the condition"? In your code, there was no condition by using "if" statement. Can you also show your code without errors?
Suppose you delete entry 5 out of 7. Then afterwards the array would be only 6 long. But you did not adjust the loop bounds, so when you reach the original 7 your index would be out of range. Also if you think about the situation more closely you will see that the entry that was in location 6 and which falls down to occupy location 5, is not having its value examined.
I means by ""cancell the condition"?" If I delete
COND= (x(i+1,j)-X).^2+(y(i+1,j)-Y).^2;
CONDD=int16(COND);
CONDD<=R^2;
x(i,:)=[];
y(i,:)=[];
This means that if my code like this
T=100;
m=10;
A=10;
B=10;
R=30;
th=0:pi/100:2*pi;
X=R*cos(th)+A;
Y=R*sin(th)+B;
x = zeros(1,m);
y = zeros(1,m);
DX=20;
for j=1:m %Number of particle
for i=1:T %Number of steps
x(i+1,j)=x(i,j)+DX*randn();
y(i+1,j)=y(i,j)+DX*randn();
end
end
plot(x,y,'k.');
hold on;
plot(X,Y,'r');
title (['Time = ', num2str(T),', Particles = ', num2str(m)]);
xlabel("space x");
ylabel("space y");
hold off;
It works but I want appliy this conditin (x(i+1,j)-X)^2-(y(i+1,j)-Y)^2<=R^2 if the condition is satisfies I want delete the points insdie the cyrcle.

Sign in to comment.

 Accepted Answer

Hello,
There are several approaches that you can try to eleminate the point inside the circle. Here, I can show you a solution
T=100;
m=10;
A=10;
B=10;
R=30;
th=0:pi/100:2*pi;
X=R*cos(th)+A;
Y=R*sin(th)+B;
x = zeros(1,m);
y = zeros(1,m);
DX=20;
for j=1:m %Number of particle
for i=1:T %Number of steps
xnext = x(i,j)+DX*randn();
ynext = y(i,j)+DX*randn();
cond = norm([xnext-A,ynext-B]);
while (cond < R)
xnext = x(i,j)+DX*randn();
ynext = y(i,j)+DX*randn();
cond = norm([xnext-A,ynext-B]);
end
x(i+1,j)=xnext;
y(i+1,j)=ynext;
end
end
plot(x(2:end,:),y(2:end,:),'k.');
hold on;
plot(X,Y,'r');
title (['Time = ', num2str(T),', Particles = ', num2str(m)]);
xlabel("space x");
ylabel("space y");
hold off;
Now, you will get rid of the previous error.

More Answers (1)

The only way matlab has to delete a single entry out of a multidimensional array is to convert the array to a column vector through linear indexing and do the deletion, leaving a column vector behind. A second index greater than 1 would then be out of range.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!