Extract elements of each cell to form a matrix and store it. Can anyone give me suggestions?
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
DGM
on 13 May 2021
For a bit more generalization
% 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};
% this is a logical mask of triu for any size
s = size(CC);
mask = triu(true(s),1);
% extract just the nonzero part of CC
CC = cellfun(@(x) x(:),CC,'UniformOutput',false);
C = [CC{mask}];
% build the outputs as a single 3D array instead of
% having a bunch of loose numbered variables
A = zeros([s size(C,1)]);
thispage = zeros(s);
for p = 1:size(C,1)
thispage(mask) = C(p,:);
A(:,:,p) = thispage;
end
% A1-An are now pages of A
A
% use them how you want
% if you don't need A for anything else, you could
% just do this while building the pages of A, i.e.
% A(:,:,p) = thispage + thispage.' + eye(s)
B = A(:,:,1) + A(:,:,1).' + eye(s)
It depends what you mean when you say "save these matrices". If you need to save them to disk, you can use save()
save('mydata.mat','A')
and then load it using load().
S = load('mydata.mat') % load into a struct
A = S.A; % explicitly allocate the desired variable
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!