How to bin a simple number array?

조회 수: 1 (최근 30일)
That One
That One 2016년 7월 25일
댓글: Steven Lord 2016년 7월 26일
Hello,
I'm reading a simple number array from a .txt file (all integers) using:
fileID = fopen('control.txt','r');
formatSpec = '%d';
A = fscanf(fileID,formatSpec);
Now I want to bin these integers into a several groups:
Group 1: integers from 21 to 100;
Group 2: integers from 101 to 500;
Group 3: integers from 501 to 1000;
Group 4: integers from 1001 to infinity (max number unknown).
I'm also interested in finding the range (how many numbers) of each group.
Thank you!

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 25일
A=1:120
[b,c]=histc(A,[21 50 60 100 inf])
out=accumarray(c'+1,(1:numel(c))',[],@(x) {A(x)})
out=out(2:end)
celldisp(out)
  댓글 수: 1
That One
That One 2016년 7월 25일
Hi Azzi,
Thanks for the quick answer. I'm actually trying to bin my number array into 4 groups and write the 4 groups into 4 .txt files as:
fileID = fopen('Group_1.txt','w');
fprintf(fileID, '%d\r\n',B);
fclose(fileID);
All I'm missing is the code to bin these numbers. Can you please show me how to do that? Thanks a lot!

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

추가 답변 (1개)

Steven Lord
Steven Lord 2016년 7월 25일
Use the histcounts function and specify a vector of edges.
  댓글 수: 4
Star Strider
Star Strider 2016년 7월 26일
You have already come close to defining them:
Group 1: integers from 21 to 100;
Group 2: integers from 101 to 500;
Group 3: integers from 501 to 1000;
Group 4: integers from 1001 to infinity (max number unknown).
Use the max function to help you define the upper edge.
Experiment with to get the result you want.
Steven Lord
Steven Lord 2016년 7월 26일
Or just specify Inf as the right endpoint of the last bin.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by