How to vectorize the following code snippet ?

M = randi([-10 10],10,10);
[m, n] = size(M);
out = zeros(m, n);
for x = 1:m
for y = 1:n
if M(x, y) > 0
out(x, y) = 1;
else if M(x, y) == 0
out(x, y) = 0;
else
out(x, y)=-1;
end
end
end

 采纳的回答

That can all be reduced to:
out = zeros(size(M))-1;
out(M>0) = 1;
out(M==0) = 0;
producing the same result.

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Simulink Functions 的更多信息

产品

版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by