Duplicate Each Element in a Matrix without using Repelem or Repmat
Mostrar comentarios más antiguos
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 comentario
Adam
el 31 de Oct. de 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.
Respuesta aceptada
Más respuestas (1)
>> 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 comentarios
Benjamin Ong
el 1 de Nov. de 2019
"This way only seems to produce a 2 by 2 everytime though no matter the size of the initial matrix"
Clearly it works, as the example in my answer shows.
Of course it can be trivially adjusted to replicate the elements of any sized array any number of times, to do so requires two very small changes to the V definition:
k = 3;
V = ceil((1:k*size(A,1))/k);
This is certainly a much simpler and more efficient solution than the one that you accepted.
Benjamin Ong
el 2 de Nov. de 2019
Ayca Yigit
el 4 de Mayo de 2021
How does it work?
Stephen23
el 4 de Mayo de 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).
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!