Diagonal displacement of matrix into another matrix

3 Ansichten (letzte 30 Tage)
Lodovico Morando
Lodovico Morando am 5 Okt. 2020
Bearbeitet: Lodovico Morando am 10 Okt. 2020
Hi,
I have this matrix 2X8:
and from this I need to create 4 matrices of zeros 5X5 where I displace the 2X8 matrix thi way:
Note that I have to do this parametrically so that if for exaple I have a matrix 2X10 it needs to be dsiplaced in 5 matrices 6X6 and so on.
Any suggestions?
  8 Kommentare
Adam Danz
Adam Danz am 6 Okt. 2020
Got it. Looks like Matt J and I answered at nearly the same time.
His approach puts the output matrices into a cell array. To access output matrix number n in his answer,
outputMatrices{n}
My approach stores the output matricies within a 3D array. To access output matrix number n in my answer,
A(:,:,n)
Lodovico Morando
Lodovico Morando am 10 Okt. 2020
Bearbeitet: Lodovico Morando am 10 Okt. 2020
Yea, using cell arrays is a good solution too, but I solved it another way anyway. Assembling together diagonal matrices. Thank you very much anyway.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 6 Okt. 2020
Bearbeitet: Matt J am 6 Okt. 2020
Call your matrix A,
A=reshape(1:16,2,[]), %example input
A = 2×8
1 3 5 7 9 11 13 15 2 4 6 8 10 12 14 16
N=size(A,2)/2;
outputMatrices=cell(1,N);
z0=num2cell(zeros(1,N));
for i=1:N
z=z0; z{i}=A(:,2*i-1:2*i);
outputMatrices{i}=blkdiag(z{:});
end
outputMatrices{:} %the resulting matrices
ans = 5×5
1 3 0 0 0 2 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ans = 5×5
0 0 0 0 0 0 5 7 0 0 0 6 8 0 0 0 0 0 0 0 0 0 0 0 0
ans = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 9 11 0 0 0 10 12 0 0 0 0 0 0
ans = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 15 0 0 0 14 16

Weitere Antworten (3)

Adam Danz
Adam Danz am 6 Okt. 2020
Bearbeitet: Adam Danz am 6 Okt. 2020
Fast & efficient vectorized method
m is the input matrix size 2xN where N is divisible by 2.
A is the output array containing the N/2 matrices along the third dimension.
% m = reshape(1:16,2,8);
m = reshape(1:20,2,10)
m = 2×10
1 3 5 7 9 11 13 15 17 19 2 4 6 8 10 12 14 16 18 20
% determine the linear index for the first 4 values (baseIdx)
nMat = size(m,2)/2;
matSize = [nMat,nMat]+1;
baseIdx = [1 2, matSize+[1,2]];
% determine the linear index of all values of m into A (Aidx)
A = nan([matSize,nMat]);
interval = 0: prod(matSize)+matSize(1)+1 : numel(A);
Aidx = baseIdx' + interval;
% Place values of m into A
A(Aidx) = m
A =
A(:,:,1) = 1 3 NaN NaN NaN NaN 2 4 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN A(:,:,2) = NaN NaN NaN NaN NaN NaN NaN 5 7 NaN NaN NaN NaN 6 8 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN A(:,:,3) = NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 9 11 NaN NaN NaN NaN 10 12 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN A(:,:,4) = NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 13 15 NaN NaN NaN NaN 14 16 NaN NaN NaN NaN NaN NaN NaN A(:,:,5) = NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 17 19 NaN NaN NaN NaN 18 20

Bruno Luong
Bruno Luong am 6 Okt. 2020
Simple loop would be the simplest for me
% Test matrix
A=randi(10,2,8)
n = size(A,2)/2
B = zeros(n+1,n+1,n);
for k=1:n
i = k+(0:1);
j = 2*k+(-1:0);
B(i,i,k) = A(:,j);
end
B

Lodovico Morando
Lodovico Morando am 10 Okt. 2020
Thank you very much all.

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by