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

 채택된 답변

Star Strider
Star Strider 2021년 2월 15일

0 개 추천

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

질문:

2021년 2월 15일

답변:

2021년 2월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by