how to vectorize for loop in this section
2 次查看(过去 30 天)
显示 更早的评论
Here's the code from a program.I try to vectorize this for loop,but I'm just a beginner in coding.Hope anyone who mastering Matlab can help.
K = 5;
M = 5;
Y = [];
g = [];
for i=1:M
Y(i) = 1;
end
while Y(M)<K+1
for j=1:1:M-1
if Y(j)>K
Y(j) = 1;
Y(j+1) = Y(j+1) + 1;
else
break;
end
end
Z = flip(Y,2);
g = [g;Z];
sum = 0;
for w=1:1:M
if(w<=K)
if(Z(w)==w)
sum = sum +1;
continue;
else
break;
end
else
if(Z(w)==K)
sum = sum + 1;
continue;
else
break;
end
end
end
if(sum==M)
break;
end
Y(1) = Y(1) + 1;
end
0 个评论
回答(1 个)
ANKUR KUMAR
2018-10-5
Refer this simple example to get your problem resolved.
A = [97 89 84; 95 82 92; 64 80 99;76 77 67;...
88 59 74; 78 66 87; 55 93 85];
mA = mean(A);
B = zeros(size(A));
for n = 1:size(A,2)
B(:,n) = A(:,n) - mA(n);
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!