how to correct this for loop code?

1 次查看(过去 30 天)
Dhines
Dhines 2013-1-18
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);

回答(1 个)

Walter Roberson
Walter Roberson 2013-1-18
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by