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!