problem with if in a matrix
1 view (last 30 days)
Show older comments
I have a matrix g:
>> g=sin((1:1:10)'*(1:1:2))
g =
0.8415 0.9093
0.9093 -0.7568
0.1411 -0.2794
-0.7568 0.9894
-0.9589 -0.5440
-0.2794 -0.5366
0.6570 0.9906
0.9894 -0.2879
0.4121 -0.7510
-0.5440 0.9129
when I add the two columns together I get
(g(:, [1])+g(:, [2]))
ans =
1.7508
0.1525
-0.1383
0.2326
-1.5029
-0.8160
1.6476
0.7015
-0.3389
0.3689
I want matlab to return the results of the first column when the results are positive and of the second column when the results are negative, so I tried:
if(g(:, [1])+g(:, [2]))>0
g(:, [1])
else
g(:, [2])
end
but that just gives me the results of the second column.
0 Comments
Accepted Answer
Azzi Abdelmalek
on 29 Sep 2013
Edited: Azzi Abdelmalek
on 29 Sep 2013
idxn=sum(g,2)<0
out=g(:,1);
out(idxn)=g(idxn,2)
0 Comments
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!