How to reconstruct a cell with different sizes from a matrix

Hi all,
I have a cell array of complex double and I want to convert them to matrix for the next process. Then, I want to reconstruct back the matrix to cell array. I do like the following codes, where I try with a simple input cells. I know this is not a good method to do, because when I try to change the input into a bigger size, it hard to manually code the position of the cells. Is there any efficient way to do this? Thank you in advance
clc
clear all
% Input cells
F = {{[0.04,0.2,0.56;0.31,0.67,0.22]},{...
[6+6j,7+3j,8-6j;6+8j,7-6j,3-3j],...
[5+6j,8+5j;6+8j,7-6j;5+6j,3-9j],...
[6+6j,7+3j,8-6j;6+8j,7-6j,3-3j],...
[5+6j,8+5j;6+8j,7-6j;5+6j,3-9j]},{...
[16+6j,7+3j,8-6j;6+8j,7-6j,3-3j],...
[16+6j,7+3j,8-6j;6+8j,7-6j,3-3j]},{...
[0+0j,0+0j;0+0j,0+0j;0+0j,0+0j]}}
% Extract the cells
G={}
dim = 2
for k=1:length(F)
G = [G,F{k}]
end
% Convert the cell to matrix
B = catpad(dim,G{:})
% Delete NaN in the matrix
V = B(~isnan(B))
% Another process apply to the matrix
%%%%%%%%%%%%%%%
% Inverse Reconstruction:
% Convert the matrix to cell
H =num2cell(V)
% Reshape the cell
M1 = {}
M = H'
for s=1:6
M1 = [M1,M{s}]
end
M1s = [M1(1) M1(3) M1(5); M1(2) M1(4) M1(6)]
M1s = cell2mat(M1s)
M2 = {}
for s=7:18
M2 = [M2,M{s}]
end
M2s{1,1}{1,1} = [M2{1,1} M2{1,3} M2{1,5};M2{1,2} M2{1,4} M2{1,6}]
M2s{1,1}{1,2} = [M2{1,7} M2{1,10}; M2{1,8} M{1,11}; M{1,9} M2{1,12}]
M3 = {}
for s=19:24
M3 = [M3,M{s}]
end
M3s = [M3(1) M3(3) M3(5); M3(2) M3(4) M3(6)]
M3ss = cell2mat(M3s)
M4 = {}
for s=25:30
M4 = [M4,M{s}]
end
M4s = [M4(1) M4(3); M4(5) M4(2); M4(4) M4(6)]
M4ss = cell2mat(M4s)
% Combine all cells to become only one cell aray
% Create 1x4 cells
C = {M1s {M2s{1,1}{1,1} M2s{1,1}{1,2}} M3ss M4ss}

11 件のコメント

Dyuman Joshi
Dyuman Joshi 2022 年 9 月 27 日
We can not run your code here, since it contains an FEX function (catpad).
Can you tell what is the expected output for the input F (as defined above)?
NHJ
NHJ 2022 年 9 月 27 日
Hi Dyuman Joshi,
The expected output is exactly the same as the input F (different values). I mean the output will be the cell array with the same size as the input. Actually, I want to apply an algorithm to the input F in matrix form. After that I want to get the output in cell array form. Thank you for your response
Dyuman Joshi
Dyuman Joshi 2022 年 9 月 27 日
I meant the output for the numerical array or the matrix form, as you mentioned it.
NHJ
NHJ 2022 年 9 月 27 日
The final output should be an image (after apply another process). Is it what you mean by 'output'? Thanks for your response
Dyuman Joshi
Dyuman Joshi 2022 年 9 月 27 日
No. I'll be more direct - What are the values/output of V and C are, with respect to the code you posted?
NHJ
NHJ 2022 年 9 月 27 日
V contains the value of input F in matrix form and C contains the value of input F in cell array form. Did I answering your question? Thank you for your response
Dyuman Joshi
Dyuman Joshi 2022 年 9 月 27 日
No, I am saying show them. In an image or as a part of code.
NHJ
NHJ 2022 年 9 月 27 日
This is the input F:
This is the output B:
This is the output V:
This is the output C:
I downloaded the file catpad and ran the code.
The ouput C doesn't match. See below -
clc
clear all
% Input cells
F = {{[0.04,0.2,0.56;0.31,0.67,0.22]},{...
[6+6j,7+3j,8-6j;6+8j,7-6j,3-3j],...
[5+6j,8+5j;6+8j,7-6j;5+6j,3-9j],...
[6+6j,7+3j,8-6j;6+8j,7-6j,3-3j],...
[5+6j,8+5j;6+8j,7-6j;5+6j,3-9j]},{...
[16+6j,7+3j,8-6j;6+8j,7-6j,3-3j],...
[16+6j,7+3j,8-6j;6+8j,7-6j,3-3j]},{...
[0+0j,0+0j;0+0j,0+0j;0+0j,0+0j]}}
F = 1×4 cell array
{1×1 cell} {1×4 cell} {1×2 cell} {1×1 cell}
% Extract the cells
G={};
dim = 2;
for k=1:length(F)
G = [G,F{k}];
end
% Convert the cell to matrix
B = catpad(dim,G{:})
B =
0.0400 + 0.0000i 0.2000 + 0.0000i 0.5600 + 0.0000i 6.0000 + 6.0000i 7.0000 + 3.0000i 8.0000 - 6.0000i 5.0000 + 6.0000i 8.0000 + 5.0000i 6.0000 + 6.0000i 7.0000 + 3.0000i 8.0000 - 6.0000i 5.0000 + 6.0000i 8.0000 + 5.0000i 16.0000 + 6.0000i 7.0000 + 3.0000i 8.0000 - 6.0000i 16.0000 + 6.0000i 7.0000 + 3.0000i 8.0000 - 6.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.3100 + 0.0000i 0.6700 + 0.0000i 0.2200 + 0.0000i 6.0000 + 8.0000i 7.0000 - 6.0000i 3.0000 - 3.0000i 6.0000 + 8.0000i 7.0000 - 6.0000i 6.0000 + 8.0000i 7.0000 - 6.0000i 3.0000 - 3.0000i 6.0000 + 8.0000i 7.0000 - 6.0000i 6.0000 + 8.0000i 7.0000 - 6.0000i 3.0000 - 3.0000i 6.0000 + 8.0000i 7.0000 - 6.0000i 3.0000 - 3.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i NaN + 0.0000i NaN + 0.0000i NaN + 0.0000i NaN + 0.0000i NaN + 0.0000i NaN + 0.0000i 5.0000 + 6.0000i 3.0000 - 9.0000i NaN + 0.0000i NaN + 0.0000i NaN + 0.0000i 5.0000 + 6.0000i 3.0000 - 9.0000i NaN + 0.0000i NaN + 0.0000i NaN + 0.0000i NaN + 0.0000i NaN + 0.0000i NaN + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
% Delete NaN in the matrix
V = B(~isnan(B))
V =
0.0400 + 0.0000i 0.3100 + 0.0000i 0.2000 + 0.0000i 0.6700 + 0.0000i 0.5600 + 0.0000i 0.2200 + 0.0000i 6.0000 + 6.0000i 6.0000 + 8.0000i 7.0000 + 3.0000i 7.0000 - 6.0000i 8.0000 - 6.0000i 3.0000 - 3.0000i 5.0000 + 6.0000i 6.0000 + 8.0000i 5.0000 + 6.0000i 8.0000 + 5.0000i 7.0000 - 6.0000i 3.0000 - 9.0000i 6.0000 + 6.0000i 6.0000 + 8.0000i 7.0000 + 3.0000i 7.0000 - 6.0000i 8.0000 - 6.0000i 3.0000 - 3.0000i 5.0000 + 6.0000i 6.0000 + 8.0000i 5.0000 + 6.0000i 8.0000 + 5.0000i 7.0000 - 6.0000i 3.0000 - 9.0000i 16.0000 + 6.0000i 6.0000 + 8.0000i 7.0000 + 3.0000i 7.0000 - 6.0000i 8.0000 - 6.0000i 3.0000 - 3.0000i 16.0000 + 6.0000i 6.0000 + 8.0000i 7.0000 + 3.0000i 7.0000 - 6.0000i 8.0000 - 6.0000i 3.0000 - 3.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
% Another process apply to the matrix
%%%%%%%%%%%%%%%
% Inverse Reconstruction:
% Convert the matrix to cell
H =num2cell(V);
% Reshape the cell
M1 = {};
M = H';
for s=1:6
M1 = [M1,M{s}];
end
M1s = [M1(1) M1(3) M1(5); M1(2) M1(4) M1(6)];
M1s = cell2mat(M1s);
M2 = {};
for s=7:18
M2 = [M2,M{s}];
end
M2s{1,1}{1,1} = [M2{1,1} M2{1,3} M2{1,5};M2{1,2} M2{1,4} M2{1,6}];
M2s{1,1}{1,2} = [M2{1,7} M2{1,10}; M2{1,8} M{1,11}; M{1,9} M2{1,12}];
M3 = {};
for s=19:24
M3 = [M3,M{s}];
end
M3s = [M3(1) M3(3) M3(5); M3(2) M3(4) M3(6)];
M3ss = cell2mat(M3s);
M4 = {};
for s=25:30
M4 = [M4,M{s}];
end
M4s = [M4(1) M4(3); M4(5) M4(2); M4(4) M4(6)];
M4ss = cell2mat(M4s);
% Combine all cells to become only one cell aray
% Create 1x4 cells
C = {M1s {M2s{1,1}{1,1} M2s{1,1}{1,2}} M3ss M4ss}
C = 1×4 cell array
{2×3 double} {1×2 cell} {2×3 double} {3×2 double}
NHJ
NHJ 2022 年 9 月 27 日
Missing another part in the code:
% Delete symmetry cells
for kk = 1:numel(F)
for ii = numel(F{kk}):-1:((numel(F{kk}))/2)+1
for oo = 1:ii-1
F{kk}(oo) = [];
break
end
end
end
Thats why the previous output C is doesn't match. At initial I want to ignore this code. By the way, is there any efficient way/method to do my previous question? Thanks for your response
Jan
Jan 2022 年 9 月 27 日
編集済み: Jan 2022 年 9 月 27 日
Just a hint: Replace
G={};
dim = 2;
for k=1:length(F)
G = [G,F{k}];
end
by
G = [F{:}];
and
M2 = {}
for s=7:18
M2 = [M2,M{s}]
end
by
M2 = [M{7:18}];
But the main problem is the switching from cells of cells to cells to numerical arrays and the other way around. What is the purpose of this confusing procedure?

サインインしてコメントする。

回答 (0 件)

製品

リリース

R2022a

質問済み:

NHJ
2022 年 9 月 27 日

編集済み:

Jan
2022 年 9 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by