Borrar filtros
Borrar filtros

How to separate matrix elements based on randomized indices

1 visualización (últimos 30 días)
Andrew Luce
Andrew Luce el 14 de Jun. de 2019
Respondida: Star Strider el 14 de Jun. de 2019
Hello so I have a matrix like such made up of x's and y'x...say it is a 2x10 array
a=[ 1 2 3 4 5 6 7.... ;
2 3 4 5 6 7 8....]
Say I want to separate the columns in the array this into three arrays with unequal amount of eleements... where the choice of which column goes into what group is random.
lets say the groups contain 5,3 and 2 elements respectfully.
how would one do this in the form of a loop
I wish to be able to plot the three groups as dots of three differnt colors to represent the groups
I was thinking of using randperm(10,10) for the random indeces for picking random columns because there would be no repeats
Thank you

Respuestas (2)

KALYAN ACHARJYA
KALYAN ACHARJYA el 14 de Jun. de 2019
Editada: KALYAN ACHARJYA el 14 de Jun. de 2019
a=rand(2,10)
[rows colm]=size(a);
rand_colm=randi(colm-3);
mat1=a(:,1:rand_colm)
mat2=a(:,rand_colm+1)
mat3=a(:,rand_colm+2:end)

Star Strider
Star Strider el 14 de Jun. de 2019
If you want, you can do it without an expressed loop (the accumarray function of course loops internally):
a = randi(9,2,10); % Create Matrix
k = randperm(10); % Column Indices
g = [ones(1,5) ones(1,3)*2 ones(1,2)*3]; % Gouping Vector
Out = accumarray(g', k', [], @(x){a(:,x)}); % Assign Outputs
FirstFive = Out{1} % Look At First Group
The grouping vector assigns the random column assignments created by randperm.

Categorías

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

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by