How to find a difference between two cell arrays containing vectors?

조회 수: 1 (최근 30일)
I have two cell arrays of unequal size containing vectors:
A={[1 1], [1 2], [1 3], [1 4]}
and
B={[1 1], [1 7], [1 3], [1 8], [1,5], [1,6]}
I want to substract A from B resulting in cell array C which will contain vectors which are NOT in both A and B. In this case wanted result would look like:
C={[1 2], [1 4], [1 5], [1 6], [1 7]}
Also, it would be good if vectors in C would be oredered in ascending order (as presented in this example).
I have tried using setdiff function but was getting error message: "Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string."
Any ideas on how to solve this?
Many thanks
  댓글 수: 2
Jan
Jan 2015년 7월 19일
"contain vectors which are NOT in both A and B": Why is [1,8] not member of C?
Aleksandar Milakovic
Aleksandar Milakovic 2015년 7월 20일
I have accidentaly not included [1 8] in the resulting vector, my mistake.

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

채택된 답변

Star Strider
Star Strider 2015년 7월 19일
This seems to work:
A={[1 1], [1 2], [1 3], [1 4]};
B={[1 1], [1 7], [1 3], [1 8], [1,5], [1,6]};
Ad = cell2mat(A');
Bd = cell2mat(B');
C{:} = setdiff(union(Bd, Ad, 'rows'), intersect(Bd,Ad,'rows'), 'rows');
The ‘C{:}’ assignment creates a cell array out of the final operation. (Note that your description of ‘C’ omitted the vector [1 8], that appears to meet the criteria.)
  댓글 수: 2
Aleksandar Milakovic
Aleksandar Milakovic 2015년 7월 20일
Thank you very much! It was my mistake not to include [1 8] in the reculting vector!

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

추가 답변 (1개)

Jan
Jan 2015년 7월 19일
A = {[1 1], [1 2], [1 3], [1 4]};
B = {[1 1], [1 7], [1 3], [1 8], [1,5], [1,6]};
Ad = cat(1, A{:});
Bd = cat(1, B{:});
C = setxor(Ad, Bd, 'rows')
But here [1,8] is member of C. Is this wanted? If not, please explain exactly which elements should be included in the result.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by