How to randomly repeat an array elements?
조회 수: 2 (최근 30일)
이전 댓글 표시
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')
댓글 수: 0
채택된 답변
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개)
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(:))'
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!