how to vectorize for loop in this section

3 ビュー (過去 30 日間)
alan huang
alan huang 2018 年 7 月 4 日
回答済み: ANKUR KUMAR 2018 年 10 月 5 日
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

回答 (1 件)

ANKUR KUMAR
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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by