How to separate matrix elements based on randomized indices
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
KALYAN ACHARJYA
2019-6-14
编辑:KALYAN ACHARJYA
2019-6-14
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)
0 个评论
Star Strider
2019-6-14
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!