フィルターのクリア

How to randomly repeat an array elements?

1 回表示 (過去 30 日間)
Ved
Ved 2013 年 10 月 20 日
コメント済み: Ved 2013 年 10 月 20 日
I have a 1x4 array as,
P = [1, -1, j, -j]
How to form a New " 1x16 random array " using only four elements of 'P' ?
This new 1x16 array should have random arrangement of elements of 'P',(i.e.,irrespective of order of elements of 'P')

採用された回答

Jos (10584)
Jos (10584) 2013 年 10 月 20 日
Here is an approach:
P = [1 -1 j -j]
N = 16 ;
ix = ceil(numel(P)*rand(1,N)) % random indices into P
Y = P(ix)
If you have access to RANDI, you could use that function as well.
If you want to have each element of P repeated four times, but all in random order, try this:
Y = repmat(P,1,4)
Y = Y(randperm(numel(Y)))
  1 件のコメント
Ved
Ved 2013 年 10 月 20 日
@Jos(10584): Thank you very much.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 20 日
P = [1, -1, j, -j];
a=perms(1:4);
id=randi(size(a,1),4,1);
idx=a(id,:);
out=P(idx(:))'
  1 件のコメント
Ved
Ved 2013 年 10 月 20 日
@Azzi: Thank you very much.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by