how to creat a Vectorization instead of a Double Loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
for i = 1:n
for j = 1:m
if A(i,j)==1
output = myfcn(A,i,j);
if output==0
A(i,j) = 2;
t = t+1;
elseif output==1
A(i,j) = 3;
t = t+1;
end
end
end
end
0 commentaires
Réponses (1)
Jan
le 4 Mar 2021
The most important part is "myfcn", which is not shown yet.
Can it be called with an array as input? What does it reply? Only 0 and 1 or are other outputs possible?
A leaner version might be:
for j = 1:m % Swap loops: Columnwise processing is faster
for i = 1:n
if A(i,j) == 1
A(i,j) = 2 + myfcn(A,i,j);
t = t+1;
end
end
end
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!