elementwise If statement, then apply an equation/function only to the elements that meet the criteria
Show older comments
Pretty self explanitory, but couldn't find an answer after much searching. i hope there's a simple solution to this:
say I have a matrix:
x = rand(3,3)
and I want to find all values in the matrix that are greater than zero and apply some elementwise equation, and for all values that are less than zero, apply a different elementwise function. like this:
if x>0
y = x.^2
elseif x<0
y = x.^3
end
is there a way to do this?
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 3 May 2013
Edited: Azzi Abdelmalek
on 3 May 2013
x = rand(3)-0.5
y=x.^3
y(x>0)=x(x>0).^2
Categories
Find more on Aerospace Blockset in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!