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 Kommentare

DGM
DGM am 15 Apr. 2022
Bearbeitet: DGM am 15 Apr. 2022
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
norh hameed am 15 Apr. 2022
I can't make these choices since the column I have is a distribution and I can't change it.
norh hameed
norh hameed am 15 Apr. 2022
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.
norh hameed
norh hameed am 15 Apr. 2022
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

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

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

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 15 Apr. 2022

0 Stimmen

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 Kommentar

norh hameed
norh hameed am 15 Apr. 2022
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.

Melden Sie sich an, um zu kommentieren.

Bruno Luong
Bruno Luong am 15 Apr. 2022
Bearbeitet: Bruno Luong am 15 Apr. 2022
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 Kommentare

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

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by