how to correct this for loop code?

1 visualización (últimos 30 días)
Dhines
Dhines el 18 de En. de 2013
for i=1:1:51
for j=1:1:51
if(i+1)>51
k=1;
else
k=i+1;
end
a1h(i,j)=A1(i,j)-A1(k,j);
end
end
%calculate vertical array
% a=input('enter the matrix');
for j=1:1:56
for i=1:1:56
if(j+1)>56
k=1;
else
k=j+1;
end
a1v(i,j)=A1(i,j)-A1(i,k);
end
end
when i run this code getting this error...
??? Attempted to access A1(52,1); index out of bounds because size(A1)=[51,56].
Error in ==> matrixupdate at 182
a1v(i,j)=A1(i,j)-A1(i,k);

Respuestas (1)

Walter Roberson
Walter Roberson el 18 de En. de 2013
In your first loop, you limit "i" and "j" to 51, but in the second loop you limit them to 56. If you want to work on a 51 x 56 array, limit i to 51 and limit j to 56.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by