Duplicate Each Element in a Matrix without using Repelem or Repmat
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Benjamin Ong
le 31 Oct 2019
Commenté : Stephen23
le 4 Mai 2021
Is there a way I can make each element in a matrix duplicate into a 3 by 3? if
A=[1 0;0 1]
Is there a way i can turn it into
Aew=[1 1 1 0 0 0;1 1 1 0 0 0;1 1 1 0 0 0;0 0 0 1 1 1;0 0 0 1 1 1;0 0 0 1 1 1]
so basically
Anew=repelem(A,3,3)
but without repelem, repmat or any special functions?
Thanks
1 commentaire
Adam
le 31 Oct 2019
Yes, but the only reason I can see for wanting to do so is as a homework question. Like most things you can do it with a for loop.
Réponse acceptée
Fangjun Jiang
le 31 Oct 2019
Modifié(e) : Fangjun Jiang
le 31 Oct 2019
a=eye(2);
k=3;
d=blkdiag(ones(k,1),ones(k,1));
b=d*a*d'
%%Or a more generic case
a=[1 2 3;4 5 6];
k=3;
[m,n]=size(a);
d1=ones(k,1)*ones(1,m);
d1=mat2cell(d1,k,ones(1,m));
d1=blkdiag(d1{:});
d2=ones(n,1)*ones(1,k);
d2=mat2cell(d2,ones(n,1),k);
d2=blkdiag(d2{:});
b=d1*a*d2
Plus de réponses (1)
Stephen23
le 31 Oct 2019
Modifié(e) : Stephen23
le 31 Oct 2019
>> A = [1,0;0,1]
A =
1 0
0 1
>> V = ceil((1:6)/3);
>> B = A(V,V)
B =
1 1 1 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
5 commentaires
Stephen23
le 4 Mai 2021
@Ayca Yigit: it generates a vector of indices V, where each index repeats k times. Consider what happens when you use those indices to select the elements of A(V,V).
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!