nested cell array into single cell array

5 次查看(过去 30 天)
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> = [4]
<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] [4] [9 4 ; 9 8 13 ; 9 14 13 ; 9 14 19] }
I hope my question is understandable
Thanks in advance
  4 个评论
Mausmi Verma
Mausmi Verma 2021-12-27
Thank you Benjamin for reply, and i got your point.
can i still get the desired output by the help of zero padding for matching the dimension
Mausmi Verma
Mausmi Verma 2021-12-27
@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
Voss 2021-12-27
编辑:Voss 2021-12-30
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]} {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
  4 个评论
Mausmi Verma
Mausmi Verma 2022-1-5
Thanks benjamin for ur support,
after applying the way mentioned above in my work i am getting my answer as:
[1,2] 2 [3,2,0;3,8,13] [4,3,2] [5,10,15] [6,1,2;6,7,2] [7,2,0;7,8,13;7,12,13] [8,3,2;8,7,2;8,13,0] <4x3 double>
[10,15] [11,12,13] [12,7,2;12,13,0;12,17,22] 13 [14,13;14,15] 15 [16,17,22;16,21,22] [17,12,13;17,18,13;17,22,0] [18,13,0;18,17,22;18,23,22] <4x3 double>
[20,15] [21,22] 22 [23,18,13;23,22,0] [24,23,22] [25,20,15]
where, <4x3 double> =
9 8 13
9 10 15
9 14 13
9 14 15
now here i have stuck with my further work as i want my answer to be like,
in place of <4x3> i want answer to be seen as [9,8,13;9,10,15;9,14,13;9,14,15]
and same goes for other <4x3 double> as [19,14,13; 19,14,15;19,18,13;19,20,15]
so finally i want my answer to look like this,
[1,2] 2 [3,2,0;3,8,13] [4,3,2] [5,10,15] [6,1,2;6,7,2] [7,2,0;7,8,13;7,12,13] [8,3,2;8,7,2;8,13,0] [9,8,13;9,10,15;9,14,13;9,14,15] [10,15] [11,12,13] [12,7,2;12,13,0;12,17,22] 13 [14,13;14,15] 15 [16,17,22;16,21,22] [17,12,13;17,18,13;17,22,0] [18,13,0;18,17,22;18,23,22] [19,14,13; 19,14,15;19,18,13;19,20,15] [20,15] [21,22] 22 [23,18,13;23,22,0] [24,23,22] [25,20,15]
Voss
Voss 2022-1-5
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by