MATLAB Answers

how to creat a Vectorization instead of a Double Loop

13 views (last 30 days)
Darcy Chou
Darcy Chou on 4 Mar 2021
Answered: Jan on 4 Mar 2021
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

Community Treasure Hunt

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

Start Hunting!