how to split arrays into different ranges?

18 Ansichten (letzte 30 Tage)
Tone&Mari
Tone&Mari am 20 Aug. 2015
Kommentiert: Finnlay am 22 Aug. 2022
The problem is; Given an array like [2,8,3,30,4,50,100,200,4,80,500]. I want to split it into three arrays with different ranges: [0-10), [10-100), and [100-1000). The above array should become:
2,8,3,4,4
30,50,80
100,200,500
how to do this?

Antworten (3)

Noam
Noam am 20 Aug. 2015
A = [2,8,3,30,4,50,100,200,4,80,500];
A(A>=0&A<10)
A(A>=10&A<100)
A(A>=100&A<1000)

Guillaume
Guillaume am 20 Aug. 2015
I would use discretize or any of the other histogram functions:
A = [2,8,3,30,4,50,100,200,4,80,500];
binindices = discretize(A, [0, 10, 100, 1000]);
splitA = arrayfun(@(bin) A(binindices == bin), 1:3, 'UniformOutput', false);
celldisp(splitA);
  1 Kommentar
matteo bottoni
matteo bottoni am 27 Mai 2020
Hi Sir,
How can I modify your code to have the same result? In my case I need to work with a matrix. So I would like to have splitted ranges on 1column keeping the rest of rows
General_matrix_sorted_A=sortrows(General_matrix,1)
col=General_matrix_sorted_A(:,1)
binindices = discretize(col, [0, 6, 12, 18, 24]);
splitA = arrayfun(@(bin) col(binindices == bin), 1:4, 'UniformOutput', false);
celldisp(splitA)
General_matrix_sorted_A is a 55x29double. There were 4 ranges.
To be clear, what I want is
split(1)=10x29double
.
.
split(4)=5X29double
Thank you so much anyway

Melden Sie sich an, um zu kommentieren.


Azzi Abdelmalek
Azzi Abdelmalek am 20 Aug. 2015
a=[2,8,3,30,4,50,100,200,4,80,500];
[~,jj]=histc(a,[0 10 100 1000 ]);
out=accumarray(jj',(1:numel(jj))',[],@(x) {a(x)});
celldisp(out)

Kategorien

Mehr zu Creating and Concatenating Matrices 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!

Translated by