how to split arrays into different ranges?

16 visualizaciones (últimos 30 días)
Tone&Mari
Tone&Mari el 20 de Ag. de 2015
Comentada: Finnlay el 22 de Ag. de 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?

Respuestas (3)

Noam
Noam el 20 de Ag. de 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 el 20 de Ag. de 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 comentario
matteo bottoni
matteo bottoni el 27 de Mayo de 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

Iniciar sesión para comentar.


Azzi Abdelmalek
Azzi Abdelmalek el 20 de Ag. de 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)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by