stacking of large matrix into stack of small column matrix

2 views (last 30 days)
Hello here i want to put row of w matrix as a column vector in w_stack for i as a number of element. i.e each row of w corresponds to each element which i want it a column vector. can you please help me
nele = 8;
w = zeros(nele,3);
w = [0 0 0;
0 0 0;
0 0 0;
0 0 0;
0 0 -2;
0 0 -2;
0 0 -2;
0 0 -2];
w_trans = w';
W = w_trans(:);
w_stack = zeros(3,1,nele);
for i = nele
w_stack(1:3,1,i) = w(1:w(1:3, 1, i)';
end

Answers (1)

DGM
DGM on 26 Nov 2022
I'm not really sure what exactly you want, but I'm going to take a guess anyway. I'm going to assume that nele is possibly less than size(w,1). I'm going to interpret your original prototype code as to be:
nele = 8;
w = [0 0 0;
0 0 0;
0 0 0;
0 0 0;
0 0 -2;
0 0 -2;
0 0 -2;
0 0 -2];
w_stack = zeros(3,1,nele);
for i = 1:nele
w_stack(:,1,i) = w(i,:);
end
w_stack
w_stack =
w_stack(:,:,1) = 0 0 0 w_stack(:,:,2) = 0 0 0 w_stack(:,:,3) = 0 0 0 w_stack(:,:,4) = 0 0 0 w_stack(:,:,5) = 0 0 -2 w_stack(:,:,6) = 0 0 -2 w_stack(:,:,7) = 0 0 -2 w_stack(:,:,8) = 0 0 -2
If that's a correct interpretation of your goal, then you can replace that with one line:
wstack = permute(w(1:nele,:),[2 3 1])
wstack =
wstack(:,:,1) = 0 0 0 wstack(:,:,2) = 0 0 0 wstack(:,:,3) = 0 0 0 wstack(:,:,4) = 0 0 0 wstack(:,:,5) = 0 0 -2 wstack(:,:,6) = 0 0 -2 wstack(:,:,7) = 0 0 -2 wstack(:,:,8) = 0 0 -2
... and the results are the same.

Categories

Find more on Loops and Conditional Statements 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!