how to split arrays into different ranges?

18 vues (au cours des 30 derniers jours)
Tone&Mari
Tone&Mari le 20 Août 2015
Commenté : Finnlay le 22 Août 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?

Réponses (3)

Noam
Noam le 20 Août 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)
  1 commentaire
Finnlay
Finnlay le 22 Août 2022
hippity hoppity,your code is now my property

Connectez-vous pour commenter.


Guillaume
Guillaume le 20 Août 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 commentaire
matteo bottoni
matteo bottoni le 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

Connectez-vous pour commenter.


Azzi Abdelmalek
Azzi Abdelmalek le 20 Août 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)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by