apply "IF" condition to a matrix whitout loop

Hi!
It is possible to use matrix operation power with "if" condition?? I want to apply "if" conditionto all value of the matrix how agree the condition with out creating a for loop or multi "if" condition
Ex: here an simple exemple I don't want to put it inside "for" loop
x = [1,2] y = [100,35]
if x>1 x = 0; else x = x .* y^2; end
In my original code "x" and "y" are huge and I try to optimize the speed
Thank you!!

 채택된 답변

Roger Stafford
Roger Stafford 2013년 11월 28일

1 개 추천

That won't work with 'if'. It won't come true unless all the elements are negative. Use logical indexing.
t = x>1;
x(t) = x(t).*y(t).^2;

댓글 수: 3

Jos (10584)
Jos (10584) 2013년 11월 28일
편집: Jos (10584) 2013년 11월 28일
Indeed, use logical indexing! However, Roger's answer should have been:
t = x>1;
x(t) = 0 ;
x(~t) = x(~t).*y(~t).^2;
Jonathan Roy
Jonathan Roy 2013년 11월 28일
It's always a pleasure to see answers popping so fast! It work so fine!!
Thank you for your help!!
Roger Stafford
Roger Stafford 2013년 11월 28일
Thanks, Jos. I guess I'm not fully awake yet.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 11월 28일

댓글:

2013년 11월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by