How to resize a matrix with zeros in between elements
Afficher commentaires plus anciens
Hello everyone.
I have matrix A and I would like to obtain matrix B.
I have used C(:,1:2)=diag(A(1,:)) but I do not know how to do it automatically because I need to use this code for a bigger matrix.
A =
10 5
4 10
2 3
B =
10 0 4 0 2 0
0 5 0 10 0 3
Réponse acceptée
Plus de réponses (1)
madhan ravi
le 24 Juil 2019
Modifié(e) : madhan ravi
le 24 Juil 2019
Try this - will work for any size of A:
A =[...
10 5
4 10
2 3];
[m,n] = size(A);
B = zeros(n,(2*m+n)-2);
c = (1:2:2*m).' + (0:n-1); % use bsxfun(@plus,(1:2:2*m).', 0:n-1) if your using version <= 2016a
r = repmat(1:n,size(c,1),1);
B(sub2ind(size(B),r,c)) = A
Gives:
B =
10 0 4 0 2 0
0 5 0 10 0 3
The below is given according to the assumption that A has n no of rows but only 2 columns, B has only 2 rows and n no of rows.
B = zeros(size(A,2),2*size(A,1));
B(1,1:2:end) = A(:,1);
B(2,2:2:end) = A(:,2)
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!