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!