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