MATLAB Answers

Extract elements of each cell to form a matrix and store it. Can anyone give me suggestions?

2 views (last 30 days)
SA
SA on 13 May 2021
Edited: SA on 13 May 2021
Hi,
I have a cell array of 4*4 size, each cell has a vector of 10 elements.
{0×0 double} {10×1 double} {10×1 double} {10×1 double}
{0×0 double} {0×0 double} {10×1 double} {10×1 double}
{0×0 double} {0×0 double} {0×0 double} {10×1 double}
{0×0 double} {0×0 double} {0×0 double} {0×0 double}
I want to extract individual elements of the cell sequentially and form a matrix like in the following. How can I form the matrix, A_1? Can you help me?
A_1 is a matrix consist of first element of each cell (as follows)=
0 x1 x2 x3
0 0 y1 y2
0 0 0 z3
0 0 0 0 ]; then the 2nd element of of every cell will form another 4*4 matrix=A_2,
3rd element of every cell will form 4*4 matrix=A_3,.....and so on up to A_10. With these A_1, A_2,...,A_10 matrices, I want to add its transpose+ Identity matrix(i.e. eye(4)). Finally, it should be like this
FullMatrix(1) = A_1+A_1(transpose)+eye(4)=
1 x1 x2 x3
x1 1 y1 y2
x2 y1 1 z3
x3 y2 z3 1;
FullMatrix(2), FullMatrix(3),......,FullMatrix(10). I've tried but failed to do it. Your support is highly appriciate. Thanks & Regards, sa

Accepted Answer

DGM
DGM on 13 May 2021
Edited: DGM on 13 May 2021
This is what I did:
% build test array
a = [1 2 3 4].';
b = [2 3 4 5].';
c = [5 4 4 5].';
d = [7 3 4 5].';
e = [1 3 4 5].';
f = [9 7 6 5].';
CC = {0 a b c;
0 0 d e;
0 0 0 f;
0 0 0 0};
% these are the indices of triu for 4x4
idx = [5 9 10 13 14 15];
% make sure vectors share the same orientation
% this may or may not be necessary depending on how CC is made
CC = cellfun(@(x) x(:),CC,'UniformOutput',false);
% extract just the nonzero part of CC
C = [CC{idx}];
% build the outputs
A1 = zeros(4,4);
A1(idx) = C(1,:)
A2 = zeros(4,4);
A2(idx) = C(2,:)
% ... and so on
At that point, you can do whatever you want with A1, A2, etc.
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!