Unique concatenation of multi-dimensional cell arrays

조회 수: 1 (최근 30일)
Milos
Milos 2023년 3월 7일
댓글: Stephen23 2023년 3월 8일
I have 15, k x k x k cell arrays, stacked ontop of each other ,where each cell contians a row vector. I would like to concactenate each row vector living in the living the the same poisition cell for all 15 cell arrays such that each of the row vectors doesn't have any repetitions
For the sake of clarity, suppose we have only two cell arrays of size 3 x 3,
A(:,:,1) = {[1;2;3],[],[3];
[],[],[4];
[],[2;3],[]}
A(:,:,2) = {[2;3;4],[4],[3];
[],[],[4];
[],[2;4],[]}
%I'd like the output to be
C = {[1;2;3;4],[4],[3];
[],[],[4];
[],[2;3;4],[]}
%This is possible to do with a for loop where each step of the for loop
%looks like
C{i} = unique(cat(1,A{i},B{i}))
However, since k can be fairly large and vectors in each cell are very large this turns out to be very inefficent for me. I don't suppose anyone knows of a quick way to do this? Many Thanks.
Edit the uniqueness is no longer an issue since if say we are able to perform the concactenation to obtain;
D = {[1;2;3;1;2;3;4],[4],[3;3];
[],[],[4;4];
[],[2;2;3;4;4],[]}
C = cellfun(@(x)unique(x), D, 'UniformOutput', false);
What still remains unsolvable to me is how one could obtain D from A. I should add that I have tried
cat(1,A(:,:,1),A(:,:,2))
cat(2,A(:,:,1),A(:,:,2))
cat(3,A(:,:,1),A(:,:,2))
none of which yeild the desired result.

채택된 답변

Matt J
Matt J 2023년 3월 7일
A(:,:,1) = {[1;2;3],[],[3];
[],[],[4];
[],[2;3],[]};
A(:,:,2) = {[2;3;4],[4],[3];
[],[],[4];
[],[2;4],[]};
C = cellfun(@union,A(:,:,1),A(:,:,2),'UniformOutput',false)
C = 3×3 cell array
{4×1 double} {[ 4]} {[ 3]} {0×1 double} {0×1 double} {[ 4]} {0×1 double} {3×1 double} {0×1 double}
  댓글 수: 1
Milos
Milos 2023년 3월 8일
Ahh great this is even simpler! I'll accept this as the awnser now since it is simpler for me.

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

추가 답변 (1개)

Voss
Voss 2023년 3월 7일
A(:,:,1) = {[1;2;3],[],[3];
[],[],[4];
[],[2;3],[]};
A(:,:,2) = {[2;3;4],[4],[3];
[],[],[4];
[],[2;4],[]};
C = cellfun(@(a,b)unique([a;b]),A(:,:,1),A(:,:,2),'UniformOutput',false)
C = 3×3 cell array
{4×1 double} {[ 4]} {[ 3]} {0×1 double} {0×1 double} {[ 4]} {0×1 double} {3×1 double} {0×1 double}
  댓글 수: 2
Milos
Milos 2023년 3월 8일
ohhh I didn't know you could do this. I'm quite new to matlab and I'm a bit confused as to how the @(a,b)unique([a;b]) part works in cellfun. Do you have anysuggestions as to where I could read more about this?
Stephen23
Stephen23 2023년 3월 8일
"Do you have anysuggestions as to where I could read more about this?"

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by