我有一个MATLAB程序,它使用两个for
循环迭代最多5次。但是,我希望MATLAB只使用(1 1), (2 2), (3 3)
等等。
以下是该计划:
syms l
a = [0 1 0 0 1 0;1 1 1 0 1 1;1 0 0 0 1 1;1 1 1 0 0 1;0 1 1 0 1 1];
n = [2 1;1 1;1 1;1 1;2 1];
for l = 1:5
for i = 1:5
j = n(l,1);
if a(i,j) == 0
a(i,j:end) = circshift(a(i,j:end),[n(l,2) n(l,2)]);
for j = n(l,1):n(l,1)+n(l,2)
a(i,n(l,1)) = 1;
end
else
a(i,j:end) = circshift(a(i,j:end),[n(l,2) n(l,2)]);
for j = n(l,1):n(l,1)+n(l,2)
a(i,n(l,1)) = 0;
end
end
break;
end
break;
end
我希望MATLAB我的程序能够像这样工作:
first l = 1 and i = 1;
second l = 2 and i = 2;
third l = 3 and i = 3;
fourth l = 4 and i = 4;
依旧......
答案 0 :(得分:1)
只需获取代码并更改内部for
循环索引,使其与外循环索引匹配。换句话说,这样做:
syms l %// Why are you doing this?
a = [0 1 0 0 1 0;1 1 1 0 1 1;1 0 0 0 1 1;1 1 1 0 0 1;0 1 1 0 1 1];
n = [2 1;1 1;1 1;1 1;2 1];
for l = 1:5
i = l; %// Change here - Remove second for loop
%// ... [rest of your code here]
%// Get rid of the break in this loop
end
%// Removed break here too
end