partition column randomly in to three columns
이전 댓글 표시
I have a column consisting of (10000) rows that I want to randomly partition it into three columns, but as you can see, (10000) doesn't divided (3) so I couldn't use the ready-made Matlab functions to partition, I was getting an error.
could you please help me.
Thank you in advance.
댓글 수: 5
You'll have to decide how you want to handle it. You can either pad the result (e.g. with zeros or NaN), or you can create three column vectors of unequal length (e.g. in a cell array), or you can interpolate the input vector (longer or shorter) such that the result is integer-divisible by 3, at which point you could just reshape or partition it.
norh hameed
2022년 4월 15일
norh hameed
2022년 4월 15일
Dyuman Joshi
2022년 4월 15일
The nearest value to 10000 that is divisible by 3 is 9999. So leave any 1 random value, and convert the rest to a 3 column matrix using reshape.
norh hameed
2022년 4월 15일
채택된 답변
추가 답변 (2개)
Walter Roberson
2022년 4월 15일
L = size(YourData, 1);
N = floor(L/3);
G = [1*ones(1,N), 2*ones(1,N), 3*ones(1,N), randperm(3,L-3*N)];
G = G(randperm(L)) ;
S1 = YourData(G==1, :);
S2 = YourData(G==2, :);
S3 = YourData(G==3, :);
Note that this selects randomly but in this particular implementation the selected columns will be in their original order within each group.
This code randomly selects which groups are slightly shorter if needed.
The three parts has "almost" equal number of elements
A=rand(10000,1); % dummy test data
G=splitapply(@(x){x},A,randi(3,size(A)))
% randomly shuffle
N = length(A);
N = floor(N/3)*3; % xomment this if you prefer not equal-length partition but none discard
G=splitapply(@(x){x},A(randperm(end,N)),mod((1:N)',3)+1);
A3=cat(2,G{:})
Then you can mix both of the above example methods.
댓글 수: 4
norh hameed
2022년 4월 15일
Bruno Luong
2022년 4월 15일
@norh hameed Who tells they must be equal in length?
norh hameed
2022년 4월 15일
Bruno Luong
2022년 4월 15일
Then I just EDIT the code
카테고리
도움말 센터 및 File Exchange에서 Linear Prediction에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!