partition column randomly in to three columns
Ältere Kommentare anzeigen
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
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
am 15 Apr. 2022
norh hameed
am 15 Apr. 2022
Dyuman Joshi
am 15 Apr. 2022
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
am 15 Apr. 2022
Akzeptierte Antwort
Weitere Antworten (2)
Walter Roberson
am 15 Apr. 2022
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
am 15 Apr. 2022
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)))
% 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 Kommentare
norh hameed
am 15 Apr. 2022
Bruno Luong
am 15 Apr. 2022
@norh hameed Who tells they must be equal in length?
norh hameed
am 15 Apr. 2022
Bruno Luong
am 15 Apr. 2022
Then I just EDIT the code
Kategorien
Mehr zu Linear Prediction finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!