필터 지우기
필터 지우기

How to shuffle some arrays of a matrix?

조회 수: 3 (최근 30일)
alexaa1989
alexaa1989 2014년 8월 12일
답변: Azzi Abdelmalek 2014년 8월 12일
Hi Does any one know how can I randomly shuffle some arrays in a matrix? for example
a=[1 2 3 4 5 6]
I need to choose from for example 2 to 5 and shuffle them like this
a=[1 5 4 2 3 6]
  댓글 수: 1
Adam
Adam 2014년 8월 12일
Do you mean you need to choose index positions 2 to 5 or actual numbers 2 to 5? Or is your start array always just a sorted contiguous sequence so they amount to the same thing?

댓글을 달려면 로그인하십시오.

답변 (5개)

Adam
Adam 2014년 8월 12일
편집: Adam 2014년 8월 12일
a = a( randperm( numel(a) ) )
works assuming a is a vector

Andrei Bobrov
Andrei Bobrov 2014년 8월 12일
a=[1 2 3 4 5 6];
n = numel(a);
a(2:n-1) = a(randperm(n-2)+1);

Michael Haderlein
Michael Haderlein 2014년 8월 12일
x=rand(1,6);
[~,y]=sort(x);
a(y)
If a is always just 1:6, you can directly use y instead.

Ahmet Cecen
Ahmet Cecen 2014년 8월 12일
p = randperm(n)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 12일
a=[1 2 3 4 5 6]
n=2:5
idx=randperm(n(end)-n(1)+1)+n(1)-1
a(n)=a(idx)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by