How to trasform from cell to matrix and transpose from horizontal to vertical at the same time?

11 Ansichten (letzte 30 Tage)
Hi all,
I have a structure with a 3x1 cell. The cell contains 1x101 matrices.
I want to access the cell and trasform it to matrix but at the same time transpose it from horizontal to vertical.
My approach:
my_matrix = cat(2, Data.my_cell{:});
results in 1x303 matrix. However I want the end product to be a 101x3 matrix. Can you help?

Akzeptierte Antwort

Chunru
Chunru am 28 Sep. 2022
Data.my_cell{1} = randn(1, 11);
Data.my_cell{2} = randn(1, 11);
Data.my_cell{3} = randn(1, 11);
Data.my_cell
ans = 1×3 cell array
{[0.2149 0.3143 1.5503 1.4478 0.1506 0.3134 -0.6636 -2.2168 -0.5194 -0.2335 -0.2900]} {[0.9692 -0.3670 0.5063 -1.1800 0.9370 -1.0615 0.8522 -0.3298 1.0530 -0.0936 -1.3496]} {[1.5188 0.9436 -0.2293 -1.3338 -0.0037 0.3350 -0.6829 -1.1980 1.1528 -2.0141 2.1318]}
my_matrix = cell2mat(Data.my_cell(:))'
my_matrix = 11×3
0.2149 0.9692 1.5188 0.3143 -0.3670 0.9436 1.5503 0.5063 -0.2293 1.4478 -1.1800 -1.3338 0.1506 0.9370 -0.0037 0.3134 -1.0615 0.3350 -0.6636 0.8522 -0.6829 -2.2168 -0.3298 -1.1980 -0.5194 1.0530 1.1528 -0.2335 -0.0936 -2.0141

Weitere Antworten (1)

J. Alex Lee
J. Alex Lee am 28 Sep. 2022
Just change your cat dimension to 1, then transpose later.
Data.my_cell = {rand(1,101);rand(1,101);rand(1,101)}
Data = struct with fields:
my_cell: {3×1 cell}
m = cat(1,Data.my_cell{:})'
m = 101×3
0.0638 0.2311 0.1824 0.7186 0.2615 0.6819 0.2611 0.6674 0.2513 0.0409 0.2923 0.7679 0.9161 0.4880 0.2248 0.0994 0.1821 0.5870 0.3398 0.8991 0.0166 0.8289 0.6311 0.8961 0.4964 0.8724 0.6765 0.9931 0.7334 0.0755
check that it is the same as having transposed each array in the cell first, then cat-ing
c = cellfun(@transpose,Data.my_cell,"uni",false);
p = [c{:}];
isequal(p,m)
ans = logical
1

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by