matrix with different randperm rows

23 visualizaciones (últimos 30 días)
Jeremy
Jeremy el 17 de Sept. de 2014
Comentada: Jeremy el 18 de Sept. de 2014
Hi,
Say I have vector of N integers, 1 through N. Is there a quick way, without loops, to create a matrix where each row is a different randperm of the vector? My N can get large (up to 100).
Thanks

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 17 de Sept. de 2014
Editada: Andrei Bobrov el 17 de Sept. de 2014
[~, out] = sort(rand(M,N),2);
  3 comentarios
Mikhail
Mikhail el 18 de Sept. de 2014
Apparently, not-))
Jeremy
Jeremy el 18 de Sept. de 2014
Works orders of magnitude faster for large group sizes :)

Iniciar sesión para comentar.

Más respuestas (3)

Guillaume
Guillaume el 17 de Sept. de 2014
cell2mat(arrayfun(@(dummy) randperm(n), 1:m, 'UniformOutput', false)');
Whether or not that can be said to be without a loop is debatable as arrayfun is effectively looping over the array 1:m

Mikhail
Mikhail el 17 de Sept. de 2014
  4 comentarios
Image Analyst
Image Analyst el 17 de Sept. de 2014
In what universe, or decade, is 100 considered large? You actually mean 100, right, like ten times ten, not 100 million or 100 billion or something?
Jeremy
Jeremy el 17 de Sept. de 2014
100 is large when you talk about some types of group coordination. I need to select k individuals from a group of N to interact at different time steps. What I've been doing is generating all combinations and randomly selecting ones to fill an interaction matrix, but that combination matrix gets big fast.

Iniciar sesión para comentar.


Joseph Cheng
Joseph Cheng el 17 de Sept. de 2014
Well there is a randperm() function that will give you a random permuation. How to get it as a MxN matrix without a loop will take some more time to think about it.
  1 comentario
Joseph Cheng
Joseph Cheng el 17 de Sept. de 2014
Editada: Joseph Cheng el 17 de Sept. de 2014
ok well it is possible to do it without a loop in 2ish lines. Use the function arrayfun() or cellfun() on an array of N*ones(M,1).

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by