Combine different size matrix

조회 수: 3 (최근 30일)
Fabian Haslwanter
Fabian Haslwanter 2023년 1월 9일
댓글: Fabian Haslwanter 2023년 1월 9일
I have 4 Matrices (4 is a Value but should be handled as a matrix):
A = [1 1 1;1 1 1;1 1 1];
B = [2 2 2;2 2 2];
C = [3 3 3];
D = [4];
vertcat(A,C,B)
ans = 6×3
1 1 1 1 1 1 1 1 1 3 3 3 2 2 2 2 2 2
They should be aligned somewhat like this:
syms A
syms B
syms C
syms D
Matrix = [0 D 0 A;0 0 D C;D 0 0 B]
Matrix = 
The right side i did with vertcat as shown above. However, i am struggling with Adding D. When its like in the 1. and 3. row, and A/B are 3/2 Row Matrices, D should become a diagonal matrix. Rest filled with 0. When its a single row like C, its just a single Value. So the final result looks like this:
Result = [0 0 4 0 0 0 1 1 1;0 0 0 4 0 0 1 1 1;0 0 0 0 4 0 1 1 1;0 0 0 0 0 4 3 3 3;4 0 0 0 0 0 2 2 2;0 4 0 0 0 0 2 2 2]
Result = 6×9
0 0 4 0 0 0 1 1 1 0 0 0 4 0 0 1 1 1 0 0 0 0 4 0 1 1 1 0 0 0 0 0 4 3 3 3 4 0 0 0 0 0 2 2 2 0 4 0 0 0 0 2 2 2
It should be done for multiple different cases, so an "automatic" way would be great.
Thank you very much!

채택된 답변

Stephen23
Stephen23 2023년 1월 9일
편집: Stephen23 2023년 1월 9일
Works for any data sets, you just need to specify the column/row order of the matrices:
C = {[1,1,1;1,1,1;1,1,1];[2,2,2;2,2,2];[3,3,3]}; % matrices A,B,C, ...etc
[~,Xc] = ismember('BAC','ABC'); % col order
[~,Xr] = ismember('ACB','ABC'); % row order
D = 4; % scalar
R = cellfun('size',C,1);
[~,Yr] = sort(repelem(Xr(Xc),R(Xc)));
M = D*eye(sum(R));
M = [M(Yr,:),vertcat(C{Xr})]
M = 6×9
0 0 4 0 0 0 1 1 1 0 0 0 4 0 0 1 1 1 0 0 0 0 4 0 1 1 1 0 0 0 0 0 4 3 3 3 4 0 0 0 0 0 2 2 2 0 4 0 0 0 0 2 2 2

추가 답변 (2개)

KSSV
KSSV 2023년 1월 9일
편집: KSSV 2023년 1월 9일
A = [1 1 1;1 1 1;1 1 1];
B = [2 2 2;2 2 2];
C = [3 3 3];
D = 4;
iwant = [diag(repmat(D,1,4),2) vertcat(A,C,B)] ;
iwant([5 12]) = D
iwant = 6×9
0 0 4 0 0 0 1 1 1 0 0 0 4 0 0 1 1 1 0 0 0 0 4 0 1 1 1 0 0 0 0 0 4 3 3 3 4 0 0 0 0 0 2 2 2 0 4 0 0 0 0 2 2 2
  댓글 수: 3
Bjorn Gustavsson
Bjorn Gustavsson 2023년 1월 9일
@Fabian Haslwanter, you should think of the different blocks as individual matrices, and then depending on what ordering you want you should build them the corresponding sizes. If you take an intermediate step for thinking you get:
syms A B C D1 D2 D3 zA1 zA2 zB1 zB2 zC1 zC2
Matrix = [zA1,D1,zA2,A;zB1,zB2,D2,B;D3,zC1,zC2,C];
Frome there you should be able to figure out what lengths you need to expand your scalar (?) D to in the different diagonal-matrices, and what sizes you need for the different zero-blocks. I think I got the first block of rows right in my answer. It should be doable to expand that snippet without too much sweat.
Fabian Haslwanter
Fabian Haslwanter 2023년 1월 9일
@Bjorn Gustavsson thank you very much. I was just working on your idea and wanted to wait until I made it to give you feedback. But I get the idea :D

댓글을 달려면 로그인하십시오.


Bjorn Gustavsson
Bjorn Gustavsson 2023년 1월 9일
If your D is a scalar that you want to expand into a diagonal matrix then perhaps you can do something along these lines:
szA = size(A);
szB = size(B);
szC = size(C);
szD = size(D);
Matrix = [zeros(szA(1),szB(2)) diag(repmat(D,szA(1))) zeros(szA(1),szC(2)) A];
and so on for the other rows. It is a bit fidgety, but this should set you on the right path.
HTH

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by