how to split arrays into different ranges?

17 views (last 30 days)
Tone&Mari
Tone&Mari on 20 Aug 2015
Commented: Finnlay on 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?

Answers (3)

Noam
Noam on 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 on 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 Comment
matteo bottoni
matteo bottoni on 27 May 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

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 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)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!