필터 지우기
필터 지우기

stacking of large matrix into stack of small column matrix

조회 수: 2 (최근 30일)
Milan
Milan 2022년 11월 26일
답변: DGM 2022년 11월 26일
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

답변 (1개)

DGM
DGM 2022년 11월 26일
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by