I have a vector that provides me with a strict order of n number. If n is 5, for example, I type
y=randperm(5)
and I get something like
1 3 5 4 2
I want to scale this order by a factor of x. If x=3, I would like to get
1 2 3 7 8 9 13 14 15 10 11 12 4 5 6
Is there an easy way to do this that I am missing? I know that
reshape(repmat(y,x=3,1),1,[])
creates
1 1 1 3 3 3 5 5 5 4 4 4 2 2 2
and then I can add 1 to 3 using a loop, but surely there is something faster that I am missing.

2 comentarios

Josué Ortega
Josué Ortega el 28 de Ag. de 2019
I have another question. The command you suggested gives me
1 2 3 7 8 9 13 14 15 10 11 12 4 5 6
is there a way to get the order of each group randomly?
So that I could get
3 1 2 9 7 8 13 14 15 11 10 12 4 6 5
Any ideas are most welcomed.
result = cell2mat(arrayfun(@(y)randperm(x)+(y-1)*x,y,'uni',false));

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 26 de Ag. de 2019
One way:
>> result = cell2mat(arrayfun(@(y)x*y-x+1:x*y,y,'uni',false))
result =
1 2 3 7 8 9 13 14 15 10 11 12 4 5 6

4 comentarios

Josué Ortega
Josué Ortega el 26 de Ag. de 2019
Many thanks for your kind answer! Just one more question: how do I invert this? Suppose I have "result", how do I get the original order?
Thanks
James Tursa
James Tursa el 26 de Ag. de 2019
Editada: James Tursa el 26 de Ag. de 2019
Do you also know x? And using result and x want to get y?
Josué Ortega
Josué Ortega el 26 de Ag. de 2019
exactly
Josué Ortega
Josué Ortega el 26 de Ag. de 2019
got it, apologies for the silly question

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 26 de Ag. de 2019

Comentada:

el 28 de Ag. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by