Index in position 2 exceeds array bounds (must not exceed 3).
1 view (last 30 days)
Show older comments
Can anyone please explain me the mistake here, I am not able to understand the error.
Theta_n=(theta:(phi/n):(theta+phi));
nNN=size(Theta_n,2);
x=zeros(n+1,1);
y=zeros(n+1,1);
for j=1:m+1
for i=((j-1)*nNN+1):j*nNN
x(i,1)=R*cos(Theta_n(1,i));
y(i,1)=R*sin(Theta_n(1,i));
end
0 Comments
Accepted Answer
Walter Roberson
on 19 Aug 2021
We are not told what n is, but we can deduce from evidence that n = 2
syms theta phi
n = 2
Theta_n=(theta:(phi/n):(theta+phi));
size(Theta_n)
nNN=size(Theta_n,2)
and we see that nNN is n+1.
We do not know what m is, so let us say m = 1 for the moment. We do not know what R is
m = 1
syms R
x=zeros(n+1,1,'like',R);
y=zeros(n+1,1,'like',R);
for j=1:m+1
for i=((j-1)*nNN+1):j*nNN
[j,i]
x(i,1)=R*cos(Theta_n(1,i));
y(i,1)=R*sin(Theta_n(1,i));
end
end
Your code is effectively trying to access Theta_n as if it were a vector intended to represent an array with nNN = (n+1) {=3 here} rows, and m+1 columns. But that is a problem because you can be sure that Theta_n only has n+1 elements, so it cannot be used to emulate an array with more than one column. If your m is greater than 0, then j can be greater than 1, and you will have problems.
And really it is a waste to hae those loops. Just do
x = reshape(R * cos(Theta_n), nNN, []);
y = reshape(R * sin(Theta_n), nNN, []);
with no looping. You will still only end up with one column in x and y, but you will do so without an error message.
If you are relying upon there ending up with m+1 columns in x and y, then your definition of Theta_n would have to change, such as if you were adding multiples of phi.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!