how to creat a Vectorization instead of a Double Loop

1 view (last 30 days)
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

Answers (1)

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

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!