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.

 採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 4 月 25 日

0 投票

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

その他の回答 (2 件)

Jan
Jan 2012 年 4 月 25 日

0 投票

A = 1:10;
n = 3;
B = reshape(repmat(A, n, 1), 1, []);
Muhammad Affandi
Muhammad Affandi 2012 年 4 月 25 日

0 投票

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 件のコメント

Oleg Komarov
Oleg Komarov 2012 年 4 月 25 日
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?).

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by