Repetition of elements in a matrix
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Danish Nasir
el 4 de Sept. de 2021
Comentada: Walter Roberson
el 4 de Sept. de 2021
Suppose i have a matrix A= [25 21 ] . I want to repeat elements by 4 and 2 times respectively. Repeat=[4 2]. However the repeated values will be in proportion i.e. Proportion=[8 17]. The sum of proportion repeated element will be equal to A matrix elements. It means that final matrix will be B= [8 8 8 1 17 4].
(25=8+8+8+1 , 21=17+4 )
Pls suggest code to generate matrix B
0 comentarios
Respuesta aceptada
Walter Roberson
el 4 de Sept. de 2021
A = [25 21 ]
Repeat = [4 2]
Proportion = [8 17]
leftover = A - Proportion .* (Repeat-1);
toRepeat = reshape([Proportion; leftover],1,[]);
repeatCount = reshape([Repeat-1; ones(1,length(Repeat))],1,[]);
B = repelem(toRepeat, repeatCount)
2 comentarios
Walter Roberson
el 4 de Sept. de 2021
The code already handles that.
A = [25 21 30]
Repeat = [4 2 1]
Proportion = [8 17 30]
leftover = A - Proportion .* (Repeat-1);
toRepeat = reshape([Proportion; leftover],1,[]);
repeatCount = reshape([Repeat-1; ones(1,length(Repeat))],1,[]);
B = repelem(toRepeat, repeatCount)
Más respuestas (0)
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!