nested cell array into single cell array

6 visualizzazioni (ultimi 30 giorni)
Mausmi Verma
Mausmi Verma il 26 Dic 2021
Commentato: Voss il 5 Gen 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> = [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 Commenti
Mausmi Verma
Mausmi Verma il 27 Dic 2021
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 il 27 Dic 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

Accedi per commentare.

Risposta accettata

Voss
Voss il 27 Dic 2021
Modificato: Voss il 30 Dic 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]} {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 Commenti
Mausmi Verma
Mausmi Verma il 5 Gen 2022
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 il 5 Gen 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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by