How to split an array into smaller unequal-sized arrays dependend on array-column values

조회 수: 7 (최근 30일)
I have an array of 2 column and about 31,000 rows. One of the two columns depicts a spatial coordinate on a grid the other one a dependent parameter. What I want to do is the following:
I need to split the array into smaller parts defined by the spatial column; let's say the spatial coordinate are ranging from 0 to 500 - I now want arrays that give me the two column values for spatial coordinate 0-10, then 10-20 and so on. This would result in 50 arrays of unequal size that cover a spatial range from 0 to 500.
Thank you
  댓글 수: 3
Adam
Adam 2019년 8월 15일
You'd be better off keeping the data in a single array and just keep another array of row indices into the array, indicating where your splits are. 50 arrays is not good to work with for numerous reasons, especially dynamic naming.
vicsm
vicsm 2019년 8월 15일
1 2
1 5
1 7
1 9
1 10
2 3
2 5
2 7
3 5
3 7
3 10
3 9
3 3
what I want is:
2 3 5
5 5 7
7 7 10
9 9
10 3

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

채택된 답변

Joel Handy
Joel Handy 2019년 8월 15일
Histcounts can be used to pin the data the way you want.
[~,~,BIN] = histcounts(mainArray(:,1), 0:10:500)
subArray1 = mainArray(bin==1,2);
subArray2 = mainArray(bin==2,2);
.
.
.
subArray50 = main array(bin==50,2)
  댓글 수: 2
Joel Handy
Joel Handy 2019년 8월 15일
I don't think OP should necessarily create individual named variables. I would actually recommend indexing into their main array using the bin indexes as needed rather than actually splitting up the data, but I wanted to show them how they would do the indexing in as simple a manner as possible and to answer their direct question.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by