how to correct this for loop code?

1 visualizzazione (ultimi 30 giorni)
Dhines
Dhines il 18 Gen 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);

Risposte (1)

Walter Roberson
Walter Roberson il 18 Gen 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.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by