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

1 vue (au cours des 30 derniers jours)
S Priya
S Priya le 19 Août 2021
Commenté : S Priya le 19 Août 2021
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

Réponse acceptée

Walter Roberson
Walter Roberson le 19 Août 2021
We are not told what n is, but we can deduce from evidence that n = 2
syms theta phi
n = 2
n = 2
Theta_n=(theta:(phi/n):(theta+phi));
size(Theta_n)
ans = 1×2
1 3
nNN=size(Theta_n,2)
nNN = 3
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
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
ans = 1×2
1 1
ans = 1×2
1 2
ans = 1×2
1 3
ans = 1×2
2 4
Error using sub2ind (line 55)
Out of range subscript.

Error in sym/subsref (line 904)
R_tilde = sub2ind(size(L), Idx.subs{:});
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.
  1 commentaire
S Priya
S Priya le 19 Août 2021
Thank you for the help sir.
I have used
nNN=size(Theta_n,2);
x = reshape(R * cos(Theta_n), nNN, []);
y = reshape(R * sin(Theta_n), nNN, []);
for x and y co-ordinates. Can I use it for z- co-ordinate too? Can you please elaborate how reshape function works?
for z- co-ordinates to be used. I have used
z=zeros(nNN,3,m+1);
for j=1:m+1
z(1:nNN,1,j)=x(:);
z(1:nNN,2,j)=y(:);
z(1:nNN,3,j)=(j-1)*L/m;
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by