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

DGM
DGM 2022년 4월 15일
편집: DGM 2022년 4월 15일
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.
I can't make these choices since the column I have is a distribution and I can't change it.
I know that some values will remain in the column and will not be included in the columns division, but I want the remainder or neglect to be as few as possible, in addition to that I want the selection to be random for columns.
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.
The problem is that the column size represents the size of the sample, i.e. it may change, it is not fixed, so I need a code that works in general, and I do not need to change or delete different values manually every time I run the program

댓글을 달려면 로그인하십시오.

 채택된 답변

Bruno Luong
Bruno Luong 2022년 4월 15일
A=rand(20,1)
A = 20×1
0.1011 0.6040 0.1700 0.4430 0.6715 0.6486 0.2120 0.1498 0.5527 0.7879
reshape(A(randperm(end,end-mod(end,3))),[],3)
ans = 6×3
0.9549 0.6715 0.1700 0.5665 0.2120 0.6040 0.6486 0.1150 0.1516 0.1011 0.6474 0.9099 0.0074 0.8325 0.4430 0.5527 0.8559 0.7879

추가 답변 (2개)

Walter Roberson
Walter Roberson 2022년 4월 15일

0 개 추천

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.

댓글 수: 1

The third column was larger than the other two columns, and I want the three columns to be the same size. I mentioned that we can neglect some values in the original column in order to divide the column equally and randomly into three columns, but we should neglect as little as possible.

댓글을 달려면 로그인하십시오.

Bruno Luong
Bruno Luong 2022년 4월 15일
편집: Bruno Luong 2022년 4월 15일
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)))
G = 3×1 cell array
{3341×1 double} {3357×1 double} {3302×1 double}
% 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{:})
A3 = 3333×3
0.9759 0.0862 0.3139 0.9268 0.2455 0.1357 0.3654 0.6463 0.1973 0.1864 0.7527 0.2257 0.4267 0.9289 0.3331 0.4444 0.4423 0.3335 0.3562 0.4052 0.4297 0.7195 0.7529 0.0198 0.4037 0.1580 0.4706 0.3445 0.0772 0.1285
Then you can mix both of the above example methods.

댓글 수: 4

Again, the size of the columns is not equal as you notice the second column contains 3334×1
@norh hameed Who tells they must be equal in length?
I need the columns to be equal because I want to combine them into one matrix, so the columns should be equal.
Then I just EDIT the code

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

2022년 4월 15일

댓글:

2022년 4월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by