how to vectorize for loop in this section

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

Réponses (1)

ANKUR KUMAR
ANKUR KUMAR le 5 Oct 2018
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

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by