필터 지우기
필터 지우기

Index into a table using a vector of values

조회 수: 5 (최근 30일)
Isabel Chen
Isabel Chen 2016년 4월 5일
댓글: Isabel Chen 2016년 4월 5일
Suppose I have a table:
T =
var1 var2
a 1
b 2
c 2
d 3
e 4
f 4
g 4
and I am interested in the rows of T where var2 takes values 1, 3, or 4. Is there a direct way to subset into the table using the vector [1; 3; 4], instead of using a for-loop? To be clear, I am interested in obtaining the following output:
subT =
var1 var2
a 1
d 3
e 4
f 4
g 4
I don't see how I can get this using a logical expression. Any thoughts?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 5일
편집: Azzi Abdelmalek 2016년 4월 5일
A=[5 1;7 3;12 4;47 4;6 4]
out=A(ismember(A(:,2),[1 3 4]),:)
  댓글 수: 1
Isabel Chen
Isabel Chen 2016년 4월 5일
Thanks Azzi, that makes sense! Your code is written for arrays -- for tables, I would do something like this:
vec = [1; 3; 4];
rows = ismember(T.var2, vec);
subT = T(rows,:);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by