How to iteratively split a matrix into multiple matrices according to a condition?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
ishbish
am 20 Feb. 2018
Kommentiert: ishbish
am 21 Feb. 2018
I have a 2 column matrix with many many rows.
It's been sorted according to the values in the first column.
I now want to split the matrix into ten matrices.
Those values in the first column which are 0-10% of the highest value will go in one matrix, those that are 10-20% of the highest value will go in the next... you get the idea.
How would I go about doing this? I presume a for loop is involved, but am struggling to make it work. Any advice would be very much appreciated - thank you!
0 Kommentare
Akzeptierte Antwort
James Tursa
am 20 Feb. 2018
Bearbeitet: James Tursa
am 20 Feb. 2018
Assuming values in 1st column are non-negative, you could put the results into a cell array:
M = your matrix
M1 = M(:,1); % 1st column
x = max(M1); % the max value
p = (0:10:100)/100 * x; % the comparison values
p(1) = -1; % Something less than 0 to catch any exact 0 values
result = cell(numel(p)-1,1);
for k=1:numel(p)-1
result{k} = M( M1>p(k) & M1<=p(k+1) , : );
end
Or if you want to "hide" the loop in the background via arrayfun:
result = arrayfun(@(x,y) M( M1>x & M1<=y , : ),p(1:end-1),p(2:end),'uni',false);
Weitere Antworten (1)
Jan
am 20 Feb. 2018
M = rand(100, 3); % Your data
[~, ~, bin] = histcounts(M(:, 1), 10); % Split first column into 10 groups
C = splitapply(@(x) {x}, M, bin); % 10 cells
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!