How to sort a cell array with respect to another cell array?

Hello:
If I have cell array A:
cellA = {'A', 'B', 'C', 'D'; 1, 2, 3, 4; 5, 6, 7, 8; 9, 1, 2, 3}
cellA = 4×4 cell array
{'A'} {'B'} {'C'} {'D'} {[1]} {[2]} {[3]} {[4]} {[5]} {[6]} {[7]} {[8]} {[9]} {[1]} {[2]} {[3]}
and cell array B:
cellB = {'C', 'A', 'D'; 1, 2, 3; 4, 5, 6; 7, 8, 9}
cellB = 4×3 cell array
{'C'} {'A'} {'D'} {[1]} {[2]} {[3]} {[4]} {[5]} {[6]} {[7]} {[8]} {[9]}
Is there a way to sort cellB with respect to cellA (specifically the first row, but with the capability to look to other rows to tiebreak)? Such that the final cellB array looks something like this, including the null spaces:
cellBFinal = {'A' '' 'C' 'D'; 2 [] 1 3; 5 [] 4 6; 8 [] 7 9}
cellBFinal = 4×4 cell array
{'A'} {0×0 char } {'C'} {'D'} {[2]} {0×0 double} {[1]} {[3]} {[5]} {0×0 double} {[4]} {[6]} {[8]} {0×0 double} {[7]} {[9]}
Thank you!

 采纳的回答

Here's one approach -
%Note that this approaches requires the 1st row of cellA to be sorted
cellA = {'A', 'B', 'C', 'D'; 1, 2, 3, 4; 5, 6, 7, 8; 9, 1, 2, 3};
cellB = {'C', 'A', 'D'; 1, 2, 3; 4, 5, 6; 7, 8, 9};
%positions of elements of row 1 from cellB which are common to elements of row 1 from cellA
[~,y]=ismember(cellA(1,:),cellB(1,:))
y = 1×4
2 0 1 3
z = any(y==0);
if z
%Concatenate empty spaces according to the class of elements with cellB
%only there is a missing common element (in this case 'B')
cellB = [cellfun(@(x) x([]), cellB(:,1), 'uni', 0) cellB];
end
%Final output
cellC = cellB(:,y+z)
cellC = 4×4 cell array
{'A'} {0×0 char } {'C'} {'D'} {[2]} {0×0 double} {[1]} {[3]} {[5]} {0×0 double} {[4]} {[6]} {[8]} {0×0 double} {[7]} {[9]}

5 个评论

A mixture of the other answer and my approach -
%Note that this approaches requires the 1st row of cellA to be sorted
cellA = {'A', 'B', 'C', 'D'; 1, 2, 3, 4; 5, 6, 7, 8; 9, 1, 2, 3};
cellB = {'C', 'A', 'D'; 1, 2, 3; 4, 5, 6; 7, 8, 9};
%positions of elements of row 1 from cellB which are common to elements of row 1 from cellA
[x,y]=ismember(cellA(1,:),cellB(1,:))
x = 1×4 logical array
1 0 1 1
y = 1×4
2 0 1 3
%Create empty cell array
cellC = repmat(cellfun(@(x) x([]), cellB(:,1), 'uni', 0), 1, size(cellA,2));
%Final output
cellC(:,x) = cellB(:,y(x))
cellC = 4×4 cell array
{'A'} {0×0 char } {'C'} {'D'} {[2]} {0×0 double} {[1]} {[3]} {[5]} {0×0 double} {[4]} {[6]} {[8]} {0×0 double} {[7]} {[9]}
Isn't this second approach exactly the same as mine? I guess with yours the empty element in the first row is a char instead of a double, but I don't know if this matters. Is there some other difference I missed?
"... but I don't know if this matters."
That's the output OP wants to obtain.
I see, thanks for the clarification.
Thank you! This method works great.

请先登录,再进行评论。

更多回答(1 个)

cellA = {'A', 'B', 'C', 'D'; 1, 2, 3, 4; 5, 6, 7, 8; 9, 1, 2, 3}
cellA = 4×4 cell array
{'A'} {'B'} {'C'} {'D'} {[1]} {[2]} {[3]} {[4]} {[5]} {[6]} {[7]} {[8]} {[9]} {[1]} {[2]} {[3]}
cellB = {'C', 'A', 'D'; 1, 2, 3; 4, 5, 6; 7, 8, 9}
cellB = 4×3 cell array
{'C'} {'A'} {'D'} {[1]} {[2]} {[3]} {[4]} {[5]} {[6]} {[7]} {[8]} {[9]}
[lia,lib] = ismember(cellA(1,:),cellB(1,:))
lia = 1×4 logical array
1 0 1 1
lib = 1×4
2 0 1 3
cellBFinal = cell(size(cellA,2),size(cellB,1))
cellBFinal = 4×4 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
cellBFinal(:,lia) = cellB(:,lib(lia))
cellBFinal = 4×4 cell array
{'A'} {0×0 double} {'C'} {'D'} {[2]} {0×0 double} {[1]} {[3]} {[5]} {0×0 double} {[4]} {[6]} {[8]} {0×0 double} {[7]} {[9]}

2 个评论

@Liam if it is important to you to have all the elements of the first row as char, then @Dyuman Joshi's somewhat more elaborate construction of the empty cell array will be needed. Otherwise can just initialize with all elements as empty doubles.
Yes, the first element does need everything to be characters (I suppose it could be converted quite easily in a later step so your method also works great).

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by