フィルターのクリア

problem with if in a matrix

2 ビュー (過去 30 日間)
Tristan
Tristan 2013 年 9 月 29 日
編集済み: Azzi Abdelmalek 2013 年 9 月 29 日
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 Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by