Azzera filtri
Azzera filtri

How to separate matrix elements based on randomized indices

1 visualizzazione (ultimi 30 giorni)
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

Risposte (2)

KALYAN ACHARJYA
KALYAN ACHARJYA il 14 Giu 2019
Modificato: KALYAN ACHARJYA il 14 Giu 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 il 14 Giu 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.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by