Obtaining data from matrix outside multiple ranges

조회 수: 1 (최근 30일)
Kosta
Kosta 2017년 1월 17일
답변: Kosta 2017년 1월 17일
Hi all,
I have a massive matrix, lets call it A:
A=[1 -2 3 4 5 6 7;
3 4 5 6 7 8 9;
4 5 6 6 8 9 10;
4 6 7 8 9 0 12;
2 11 5 7 2 7 21];
and I would like to obtain any row(s), where the 2nd column's data is within these two ranges: 0<A(:,2)<5 and 10<A(:,2)<15.
I know how to do this for one specified range, but not for two different ranges like above.
  댓글 수: 1
Stephen23
Stephen23 2017년 1월 17일
@Kosta: your example is not very good because all rows contain a value within the limits that your specify. You should add some rows that do not fulfill these conditions.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2017년 1월 17일
편집: Stephen23 2017년 1월 17일
Use the or operator |:
A = [1 -2 3 4 5 6 7;
3 4 5 6 7 8 9;
4 5 6 6 8 9 10;
4 6 7 8 9 0 12;
2 11 5 7 2 7 21];
%
idx = 0 < A(:,2) < 5;
idy = 10 < A(:,2) < 15;
B = A(idx | idy, :)
giving
B =
1 -2 3 4 5 6 7
3 4 5 6 7 8 9
4 5 6 6 8 9 10
4 6 7 8 9 0 12
2 11 5 7 2 7 21
(All rows of your example matrix fulfill these conditions)

추가 답변 (1개)

Kosta
Kosta 2017년 1월 17일
Works perfectly, thanks for your help!

카테고리

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