nested cell array into single cell array

조회 수: 6 (최근 30일)
Mausmi Verma
Mausmi Verma 2021년 12월 26일
댓글: Voss 2022년 1월 5일
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