how to split arrays into different ranges?

조회 수: 18 (최근 30일)
Tone&Mari
Tone&Mari 2015년 8월 20일
댓글: Finnlay 2022년 8월 22일
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?

답변 (3개)

Noam
Noam 2015년 8월 20일
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
Finnlay
Finnlay 2022년 8월 22일
hippity hoppity,your code is now my property

댓글을 달려면 로그인하십시오.


Guillaume
Guillaume 2015년 8월 20일
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
matteo bottoni
matteo bottoni 2020년 5월 27일
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

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 2015년 8월 20일
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)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by