how to vectorize for loop in this section
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
ANKUR KUMAR
on 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
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!