# nested cell array into single cell array

1 view (last 30 days)
Mausmi Verma on 26 Dec 2021
Commented: Voss on 5 Jan 2022
A={<1x1 cell> <1x3 cell> 4 <1x4 cell> }
<1x1>= A{1, 1}{1, 1}{1, 1} <1x3 double> =[2 3 4]
<1x3>= A{1, 2}{1, 1} <0x0 double> = [ ]
A{1, 2}{1, 2} <1x2 double> = [ 3 4]
A{1, 2}{1, 3}{1, 1} <0x0 double> =[ ] A{1, 2}{1, 3}{1, 2} <1x3 double> =[3 8 13]
4= A{1, 3} <1x1 double> = 
<1x4>= A{1, 4}{1, 1} <1x2 double> = [9 4]
A{1, 7}{1, 2}{1, 1} <0x0 double> =[ ]
A{1, 7}{1, 2}{1, 2} <1x3 double> =[ 9 8 13 ]
A{1, 7}{1, 3} <0x0 cell> = [ ]
A{1, 7}{1, 4}{1, 1} <1x3 double> = [ 9 14 13 ] A{1, 7}{1, 4}{1, 2} <1x3 double> =[ 9 14 19 ]
and based on the output given above, i want my final answer to look like this:
B={ [2 3 4] [3 4; 3 8 13]  [9 4 ; 9 8 13 ; 9 14 13 ; 9 14 19] }
I hope my question is understandable
Mausmi Verma on 27 Dec 2021
@Image Analyst thank you for pointing out that but its exactly not the same because the pattern of the desired output is a bit different

Voss on 27 Dec 2021
Edited: Voss on 30 Dec 2021
As far as I can tell, the variable A has this structure (I'm going to assume those 7's in the question should actually be 4's):
A = { ...
{{[2 3 4]}} ...
{[] [3 4] {[] [3 8 13]}} ...
4 ...
{[9 4] {[] [9 8 13]} [] {[9 14 13] [9 14 19]}} ...
};
To verify each entry (and leaving in redundant row index "1," throughout, for consistency with the notation in the question):
format('compact');
A{1,1}
ans = 1×1 cell array {1×1 cell}
A{1,1}{1,1}
ans = 1×1 cell array {[2 3 4]}
A{1,1}{1,1}{1,1}
ans = 1×3
2 3 4
A{1,2}
ans = 1×3 cell array {0×0 double} {[3 4]} {1×2 cell}
A{1,2}{1,1}
ans = []
A{1,2}{1,2}
ans = 1×2
3 4
A{1,2}{1,3}
ans = 1×2 cell array {0×0 double} {[3 8 13]}
A{1,2}{1,3}{1,1}
ans = []
A{1,2}{1,3}{1,2}
ans = 1×3
3 8 13
A{1,3}
ans = 4
A{1,4}
ans = 1×4 cell array {[9 4]} {1×2 cell} {0×0 double} {1×2 cell}
A{1,4}{1,1}
ans = 1×2
9 4
A{1,4}{1,2}
ans = 1×2 cell array {0×0 double} {[9 8 13]}
A{1,4}{1,2}{1,1}
ans = []
A{1,4}{1,2}{1,2}
ans = 1×3
9 8 13
A{1,4}{1,3}
ans = []
A{1,4}{1,4}
ans = 1×2 cell array {[9 14 13]} {[9 14 19]}
A{1,4}{1,4}{1,1}
ans = 1×3
9 14 13
A{1,4}{1,4}{1,2}
ans = 1×3
9 14 19
Below is a function you can use to do what you want. You can call it like this to get the B you want from the A you have:
B = cell(size(A));
for i = 1:numel(A)
B{i} = build_matrix(A{i});
end
display(B)
B = 1×4 cell array {[2 3 4]} {2×3 double} {} {4×3 double}
B{:}
ans = 1×3
2 3 4
ans = 2×3
3 4 0 3 8 13
ans = 4
ans = 4×3
9 4 0 9 8 13 9 14 13 9 14 19
function M = build_matrix(C,M)
if nargin < 2
M = [];
end
if iscell(C)
for i = 1:numel(C)
M = build_matrix(C{i},M);
end
elseif ~isempty(C)
if ~isempty(M)
sC = size(C,2);
sM = size(M,2);
if sM < sC
M(:,end+1:sC) = 0;
elseif sM > sC
C(:,end+1:sM) = 0;
end
end
M = [M; C];
end
end
% function M = build_matrix(C,M)
% if nargin < 2
% M = [];
% end
% if iscell(C)
% for i = 1:numel(C)
% M = build_matrix(C{i},M);
% end
% else
% if ~isempty(M) && size(M,2) ~= size(C,2)
% M(:,end+1:size(C,2)) = 0;
% end
% M = [M; C];
% end
% end
Voss on 5 Jan 2022
Looks like that's just how MATLAB prints it to the command line, not a difference between the answer you get and the answer you want.