필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

problem in vectorize script

조회 수: 2 (최근 30일)
Mani Ahmadian
Mani Ahmadian 2014년 10월 19일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi
I want to improve my script as:
a=round(50*rand(5,7)+50);
for ii=1
for jj=1
if a(ii,jj)>= 60
b1=2*a(ii,jj)
else
b2=-1*a(ii,jj)
end
end
end
b=b1+b2;
to vetcorized. How can I do that?
Thanks a lot
Mani
  댓글 수: 1
Jan
Jan 2014년 10월 19일
for ii=1 runs the loop over one element only. Do you mean for ii=1:5 ?

답변 (2개)

Rick Rosson
Rick Rosson 2014년 10월 19일
a = round(50*rand(5,7)+50);
b = 2*a;
b(a<60) = -a;
  댓글 수: 1
Jan
Jan 2014년 10월 19일
The right hand sige need to be masked also:
b(a<60) = -a(a<60);

Jan
Jan 2014년 10월 19일
Or as a one-liner:
b = a .* ((a >= 60) * 3 - 1);

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by