how to creat a Vectorization instead of a Double Loop

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

回答(1 个)

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

类别

在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

回答:

Jan
2021-3-4

Community Treasure Hunt

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

Start Hunting!

Translated by