nested cell array into single cell array
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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, 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
Risposta accettata
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}
A{1,1}{1,1}
A{1,1}{1,1}{1,1}
A{1,2}
A{1,2}{1,1}
A{1,2}{1,2}
A{1,2}{1,3}
A{1,2}{1,3}{1,1}
A{1,2}{1,3}{1,2}
A{1,3}
A{1,4}
A{1,4}{1,1}
A{1,4}{1,2}
A{1,4}{1,2}{1,1}
A{1,4}{1,2}{1,2}
A{1,4}{1,3}
A{1,4}{1,4}
A{1,4}{1,4}{1,1}
A{1,4}{1,4}{1,2}
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{:}
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
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!