Duplicate elements in array n times, where n is an array itself
Mostrar comentarios más antiguos
Hi everyone,
I have two arrays of the same length and would like to duplicate each element in the first array n times, where n is the value in the second array. Example:
A = [1 2 3]
B = [2 3 1]
then C shall be = [1 1 2 2 2 3]
How can this be done? I checked the forum and tried to play around with repmat but could not get it to work.
Any help is greatly appreciated! Thanks!
1 comentario
Azzi Abdelmalek
el 2 de Sept. de 2016
You mean how to duplicate (not to multiply)
Respuesta aceptada
Más respuestas (1)
Azzi Abdelmalek
el 2 de Sept. de 2016
A = [1 2 3]
B = [2 3 1]
C=cell2mat(arrayfun(@(x,y) repmat(x,1,y),A,B,'un',0))
Categorías
Más información sobre Image Arithmetic 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!