How can I find values within a range of one column in a two column matrix, and get the value plus the corresponding value in the other column?

조회 수: 8 (최근 30일)
Hello,
Let's say I have a simple matrix:
A = [ 1 6; 2 6; 3 7; 4 7; 5 8; 6 4; 7 2]
A =
1 6
2 6
3 7
4 7
5 8
6 4
7 2
and I want to find the values in the second column which are within a range:
ValuesIwant = A(A >= 4 & A <= 6) <<--- My first issue is i'm not sure how to say look in the second column only
Assuming that's a simple fix, how do I ask it to return the value in the second column ALONG with the corresponding value in the first column?
Let's say ValuesIwant =
6
6
4
How do I get into a NEW array (or matrix, etc):
1 6
2 6
6 4
Thanks for the help!

채택된 답변

sixwwwwww
sixwwwwww 2013년 11월 15일
Dear MacKanzie,
You can do something like this:
A = [ 1 6; 2 6; 3 7; 4 7; 5 8; 6 4; 7 2];
a = find(ismember(A(:, 2), [4, 6]));
B = A(a, :);
I hope it helps. Good luck!

추가 답변 (2개)

Roger Stafford
Roger Stafford 2013년 11월 15일
A(A(:,2)>=4&A(:,2)<=6,:)

MacKenzie
MacKenzie 2013년 11월 15일
thank you both!

카테고리

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