필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by