Selecting only some rows of a matrix

조회 수: 1 (최근 30일)
MRC
MRC 2013년 12월 12일
답변: Andrei Bobrov 2013년 12월 12일
Hi, I have a matrix A mxn and I want to select only some of its rows satisfying this criterion: A(i,1)<=1e-03 && A(i,1)>=-1e-03 && A(i,2)<=1e-03 A(i,2)>=-1e-03 && A(i,3:end)<= 1e-03. I can't use loops.
Could you help me? Thanks

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 12일
idx=A(:,1)<=1e-03 & A(:,1)>=-1e-03 & A(:,2)<=1e-03 & A(:,2)>=-1e-03 & all(A(:,3:end)<= 1e-03,2)
B=A(idx,:)

추가 답변 (2개)

Simon
Simon 2013년 12월 12일
편집: Simon 2013년 12월 12일
Hi!
So, what is "i"? Did you mean ":"? I assume ":" here.
Start by looking at the first criterion, you can write
crit1 = A(:, 1) <= 1e-3;
This will give you a logical vector. Do the same with the other criterions and combine them.
Hint: For the last criterion the function "all" is useful.

Andrei Bobrov
Andrei Bobrov 2013년 12월 12일
A(all(A(:,1:2) >= -1e-3,2) & all(A <= 1e-3,2),:)

카테고리

Help CenterFile Exchange에서 Oceanography and Hydrology에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by