Extract or keep rows based on an array.

I have been trying multible things and researching on the MathWorks site for hours for what should be a very simple thing I believe. So, I have a table of data that contains three columns. In column one there is are values that are repeated but each row is unique. For example:
A = [1 'cat' 24; 1 'dog' 2; 2 'horse' 10; 2 'dog' 3; 3 'sheep' 45; 3 'cow' 203; 4 'cow' 233; 5 'pig' 23; 5 'walrus' 2; 6 'horse' 4]
And I have a vector of values B = [2 4 5]
I want to pull out or keep only the rows where the value in column one of A is the same as any one of the values in the vector B.
In short, want to parse down A from the above to a leaner version with only the data I need. A = [2 'horse' 10; 2 'dog' 3; 4 'cow' 233; 5 'pig' 23; 5 'walrus' 2]
My real example is 3,142 rows in size what should parse down to around 861.

回答(2 个)

Kellie - if we can assume that your A is constructed as a cell array like
A = {1 'cat' 24; 1 'dog' 2; 2 'horse' 0; 2 'dog' 3; 3 'sheep' 45; 3 'cow' 203; 4 'cow' 233; 5 'pig' 23; 5 'walrus' 2; 6 'horse' 4}
then we can find those rows whose first column element is in the set defined by B by evaluating
A(cell2mat(arrayfun(@(x)ismember(x,B), cell2mat(A(:,1)), 'UniformOutput', false)),:)
which returns
[2] 'horse' [ 0]
[2] 'dog' [ 3]
[4] 'cow' [233]
[5] 'pig' [ 23]
[5] 'walrus' [ 2]
In the above line of code, we extract the first column of A and convert it from a cell array into a matrix
cell2mat(A(:,1))
We then use arrayfun to evaluate each element of this column to see if it is a member of B
arrayfun(@(x)ismember(x,B), cell2mat(A(:,1)), 'UniformOutput', false)
Each call to ismember returns a logical zero (not a member) or one (is a member). We then convert this to a matrix and extract those rows of A that are members of B (so the logical zero means the row will be ignored).
Try the above and see what happens!

3 个评论

I do not understand. A is a table/matrix. In my real problem, it is a 3142 x 3 table. Does this still apply. The cell2mat is not making sense to me.
result:
Error using cell2mat (line 42) You cannot subscript a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts). Use a row subscript and a variable subscript.
Kellie - it wasn't clear what your A was (to me) so I assumed that it could be written as a cell array. You can try converting your table into a cell array with table2cell and then trying the above code.

请先登录,再进行评论。

A = {1 'cat' 24; 1 'dog' 2; 2 'horse' 10; 2 'dog' 3; 3 'sheep' 45; 3 'cow' 203; 4 'cow' 233; 5 'pig' 23; 5 'walrus' 2; 6 'horse' 4};
B = [2 4 5];
out = A(ismember([A{:,1}],B),:);

8 个评论

Error using cell/ismember (line 34) Input A of class cell and input B of class double must be cell arrays of character vectors, unless one is a character vector.
This actually gets me right back to where I had been two or three hours ago. :(
>> indx = poverty(ismember([poverty(:,1)],rfstates),:); Error using tabular/ismember (line 30) A and B must both be tables.
So, I transformed rfstates into a table rfs, and the result is:
>> indx = poverty(ismember([poverty(:,1)],rfs),:); Error using tabular/ismember (line 35) A and B must contain the same variables.
@Kellie Anton: Please attach your data's example (mat - file).
>> A = {1 'cat' 24; 1 'dog' 2; 2 'horse' 10; 2 'dog' 3; 3 'sheep' 45; 3 'cow' 203; 4 'cow' 233; 5 'pig' 23; 5 'walrus' 2; 6 'horse' 4};
B = [2 4 5];
out = A(ismember([A{:,1}],B),:)
out =
5×3 cell array
[2] 'horse' [ 10]
[2] 'dog' [ 3]
[4] 'cow' [233]
[5] 'pig' [ 23]
[5] 'walrus' [ 2]
>>
out = poverty(ismember(str2double(poverty.State),rfstates),:)
That nailed it. Thank you!
I knew it had to be easy! I was missing the formatting issue I think.
Here it is customary to "accepted" the answer that solve your problem...

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by