Distribution in Array

Hi..
Let say i have a array A=1:10; I want every element in array A to distribute into array C for n-times. C will be like this: (eg n=3)
C=[1 1 1 2 2 2 3 3 3 4 4 4 .... 10 10 10]
Is there any function to do that?
What i did was like this:
A=1:10;
n=1:3;
C=sparse(length(n),length(A));
for i=1:length(A) C(:,i)=A(i); end
C=reshape(C,1,length(A)*length(n));
And i'm not working with array's length of 10. Instead about 10000000 and every element to repeat itself 15 times. Really keen to avoid using 'for' loops but dont know how.
Im new to matlab. Thanks in advance for your help.

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 25 de Abr. de 2012

0 votos

A = 1:10;
n = 3;
C = kron(A,ones(1,n));

Más respuestas (2)

Jan
Jan el 25 de Abr. de 2012

0 votos

A = 1:10;
n = 3;
B = reshape(repmat(A, n, 1), 1, []);
Muhammad Affandi
Muhammad Affandi el 25 de Abr. de 2012

0 votos

Thanks to both ans. But still it return ??? Out of memory. Type HELP MEMORY for your options.
Basically my work is like this:
A=1:16885575; n=16;
Both 'kron' and 'reshape' give out of memory statement. So any other alternative or suggestion?

1 comentario

Oleg Komarov
Oleg Komarov el 25 de Abr. de 2012
It should be repmat not reshape.
Also, consider that:
16885575 * 16 * (8 bytes) = 2.0129174 gigabytes
Do you have a contiguous block ot that size? Otherwise you'll keep getting that error.
The main question is, what do you need such an array for? Maybe you could use loops (why do you want to avoid them?).

Iniciar sesión para comentar.

Categorías

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by