Borrar filtros
Borrar filtros

How to shuffle some arrays of a matrix?

3 visualizaciones (últimos 30 días)
alexaa1989
alexaa1989 el 12 de Ag. de 2014
Respondida: Azzi Abdelmalek el 12 de Ag. de 2014
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 comentario
Adam
Adam el 12 de Ag. de 2014
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?

Iniciar sesión para comentar.

Respuestas (5)

Adam
Adam el 12 de Ag. de 2014
Editada: Adam el 12 de Ag. de 2014
a = a( randperm( numel(a) ) )
works assuming a is a vector

Andrei Bobrov
Andrei Bobrov el 12 de Ag. de 2014
a=[1 2 3 4 5 6];
n = numel(a);
a(2:n-1) = a(randperm(n-2)+1);

Michael Haderlein
Michael Haderlein el 12 de Ag. de 2014
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 el 12 de Ag. de 2014
p = randperm(n)

Azzi Abdelmalek
Azzi Abdelmalek el 12 de Ag. de 2014
a=[1 2 3 4 5 6]
n=2:5
idx=randperm(n(end)-n(1)+1)+n(1)-1
a(n)=a(idx)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by