elementwise If statement, then apply an equation/function only to the elements that meet the criteria

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

For this specific example:
x = magic(3)-5;
idx = sign(x);
x(idx==1) = x(idx==1).^2;
x(idx==-1) = x(idx==-1).^3;
In general you can use logical indexing to turn any type of logical expression into an index.

11 Comments

you mean it should be like this, since y is the new variable:
x = magic(3)-5;
idx = sign(x);
y(idx==1) = x(idx==1).^2;
y(idx==-1) = x(idx==-1).^3;
it seems to work, but it gives me a vector for y...
make y a copy of x in the beginning and perform the same operations, it will be a matrix.
hmmm again running into the same problem as below. what if none of the elements are less than zero or conversely, greater than zero? it returns an error...
x = magic(3); %all values greater than zero
idx = sign(x);
x(idx==1) = x(idx==1).^2;
x(idx==-1) = x(idx==-1).^3;
%no error
I don't know what you're modifyign byut this does not return an error. The only way it would return an error is if you do not have y defined before the code runs.
you should always do this.
y = zeros(size(x));
y(etc.)
So in total:
x = magic(3); %all values greater than zero
idx = sign(x);
y = zeros(size(x));
y(idx==1) = x(idx==1).^2;
y(idx==-1) = x(idx==-1).^3;
not sure what is going on, i think the problem is that the operations i'm using in the code are with other matrices that are the same size as the original x so it returns an error. i don't know what to do about that, because they have to be the same size.
ex:
x = magic(3); %all values greater than zero
z = magic(3)
idx = sign(x);
y = zeros(size(x));
y(idx==1) = z.*x(idx==1).^2;
y(idx==-1) = x(idx==-1).^3;
You could also combine both into one operation...maybe that would avoid your errors?
x = 2*rand(3,3)-1; %some positive, some negative
idx = (x>0); %1 where x>0, 0 otherwise
y = idx.*x.^2 + ~idx.*x.^3;
Note that x==0 will be treated just like x<0, which for this case is fine.
interesting solution. let me give that a try. it's a little funky for my application but if it works who cares, right?
that looks to have done it. many thanks to all of you. i can't believe this simple thing ended up being so difficult in the end.

Sign in to comment.

More Answers (1)

x = rand(3)-0.5
y=x.^3
y(x>0)=x(x>0).^2

2 Comments

OK but what happens if there are no values of x>0 then the third line will be empty and thus generate an error
I did this to get around it, but if you've got a more elegant way, I'm all ears:
if x <=0
y=x.^3
else
y= x.^3
y(x>0)=x(x>0).^2
end

Sign in to comment.

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!