problem with if in a matrix

9 次查看(过去 30 天)
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.

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-9-29
编辑:Azzi Abdelmalek 2013-9-29
idxn=sum(g,2)<0
out=g(:,1);
out(idxn)=g(idxn,2)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Deep Learning Toolbox 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by