필터 지우기
필터 지우기

Find matching entries in cell array

조회 수: 16 (최근 30일)
Seb
Seb 2017년 7월 17일
댓글: Walter Roberson 2017년 7월 18일
If I have a cell array full of character arrays, I want to return a cell array of all the items that are common between all columns. For example:
Sample data:
example{1,1}{1,1} = 'a'
example{1,1}{2,1} = 'b'
example{1,1}{3,1} = 'c'
example{1,2}{1,1} = 'a'
example{1,2}{2,1} = 'b'
example{1,3}{1,1} = 'x'
example{1,3}{2,1} = 'b'
example{1,3}{3,1} = 'a'
I then want to return a Nx1 cell array with the values that match between all items, so it would return [a,b] - (order is not important).
The example data must be assumed to be any size.
I have tried strcmp and a few other methods but cant get my head around it! Any help appreciated.
  댓글 수: 2
John
John 2017년 7월 17일
In the example you provided, "a" and "b" are not common to all. example{1,4} does not contain those values.
Can you clarify what you are trying to achieve?
Seb
Seb 2017년 7월 18일
sorry that was a mistake, disregard those two bottom lines of code

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 7월 17일
result = example{1,1};
for K = 2 : numel(example)
result = intersect(result, example{K}, 'rows');
end
At the end, only the items in common with everything will be left. But as John points out, that will be empty.
  댓글 수: 3
Seb
Seb 2017년 7월 18일
just note: it throws a warning: The 'rows' input is not supported for cell array inputs.
Just wondering if this is fine/any way to resolve this...?
Walter Roberson
Walter Roberson 2017년 7월 18일
Just remove 'rows' as an option.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by