index multiple columns together on different critera

조회 수: 12 (최근 30일)
Jacob Matson
Jacob Matson 2020년 8월 13일
편집: madhan ravi 2020년 8월 13일
I have a maxtrix of 2 columns, I want to pull out a new matrix with all the rows where column 1 is <0.2 and column 2 is >0.2. The row values are matched, both columns need to stay together.
A= [0.09 0.48
0.5 0.1
0.04 0.3]
I want make the new matrix to return
B= [0.09 0.48
0.04 0.3]
I've tried things like these, but I only end up with the first column
idx=A(:,1)<0.2 | A(:,2)>0.2;
B=A(idx)
B=[0.09
.04]
How do I return column 2 as well?

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 13일

추가 답변 (2개)

hosein Javan
hosein Javan 2020년 8월 13일
A= [0.09 0.48
0.5 0.1
0.04 0.3];
idx1 = A(:,1)<0.2 ; idx2 = A(:,2)>0.2;
B(:,1) = A(idx1,1); B(:,2) = A(idx2,2);
% answer in command window
B =
0.0900 0.4800
0.0400 0.3000

madhan ravi
madhan ravi 2020년 8월 13일
편집: madhan ravi 2020년 8월 13일
“I want to pull out a new matrix with all the rows where column 1 is <0.2 and column 2 is >0.2.”
idx = A(:,1)<0.2 & A(:,2)>0.2;
A(idx, :)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by