필터 지우기
필터 지우기

using ismember to select row

조회 수: 12 (최근 30일)
Matthew
Matthew 2014년 6월 29일
댓글: Matthew 2014년 6월 29일
I am trying to use ismember, but not exactly sure it will do what i need.
I have an matrix of indices, not necessarily a sequential array, of values from 0 to 10.
A=[0 1 2 3 6 9 10]
I have a second array, a 3 column matrix, that is made of the values of A. Example below.
B= [0 1 3;
3 6 9;
0 3 8;
5 4 9]
I am looking for a way to select only those rows in B, that contain the values listed in A. The values in A can be used more than once, but all 3 columns have to have one of those values.
  댓글 수: 2
Image Analyst
Image Analyst 2014년 6월 29일
편집: Image Analyst 2014년 6월 29일
Since B is made up of values from A, then tell me how any row will not be selected? Can B contain other values, other than what is in A? You did not mention that that was possible, only that B is created from values in A. I just don't see anyway that all rows of B will not get selected unless you're not telling us something.
Matthew
Matthew 2014년 6월 29일
I think i failed to mention that the values in A are used three different times in the array once for each column. Each of these 3 values are identifying an index for a triangle.

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

채택된 답변

Roger Stafford
Roger Stafford 2014년 6월 29일
r = find(all(ismember(B,A),2));
The values in 'r' are indices of rows of B consisting entirely of element of A.
  댓글 수: 1
Matthew
Matthew 2014년 6월 29일
This worked in combination with newarray=B(r,:), to create the new matrix.
thank you.

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

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2014년 6월 29일
You can try the following
A = [0 1 2 3 6 9 10]
B = [0 1 3;3 6 9;0 3 8; 5 4 9]
B(arrayfun(@(x)all(ismember(B(x,:),A)),1:size(B,1))',:)
ans =
0 1 3
3 6 9
The code uses the anonymous function @(x)all(ismember(B(x,:),A)) to check to see which elements of row x are members of A ( ismember(B(x,:),A) ) returning ones for those elements in the row that are in A and zeros otherwise.
all is used to see if all elements are non-zero - so if a one is returned, then that means that all elements in that row of B are in A.
arrayfun is used to apply the anonymous function to each row of B using the row indices of 1:size(B,1). The transpose of this column vector produces a row vector of zeros and ones where the former indicates that not all elements of that row of B are in A and the latter indicates that all elements of that row of B are in A. We use this to grab those rows of B whose elements are all in A.
  댓글 수: 1
Matthew
Matthew 2014년 6월 29일
편집: Matthew 2014년 6월 29일
Thank you, this is working somewhat, but it seems to not be creating a full array. My test array (A) ranges from 0-143(sequential), my other array (B) should contain row that are not being included.
It is only showing the first 144 rows that fit the criteria, but it stops there and it is clear that is should contain more, closer to 250 or so. It seems like i have 144 values to check and is only return the first 144 rows that share values.
Any ideas why? You test on my small example worked perfect and is exactly what i need. Thank you.

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

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by