Splitting an array into smaller unequal-sized arrays dependend on array-column values
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello all, I'm quite new to MatLab and this problem really drives me insane:
I have a huge 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. 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.
II. Secondly, I would need to calculate the average values of the resulting columns of every single array so that I obtain per array one 2-dimensional point.
III. Thirdly, I could plot these points and I would be super happy.
Sadly, I'm super confused since I miserably fail at step I. - Maybe there is even an easier way than to split the giant array in so many small arrays - who knows..
I would be really really happy for any suggestion.
Thank you,
Arne
댓글 수: 2
Guillaume
2014년 11월 7일
So, do you actually need the values of the generated sub arrays, or just their average value? Just the average values is easier.
채택된 답변
Guillaume
2014년 11월 7일
편집: Guillaume
2014년 11월 7일
Using the new histcounts (it works the same with histc)
m=[1 2;2 5;4 7;5 10;6 9;3 3]
[~, ~, indices ] = histcounts(m(:, 2), 0:2:max(m(:, 2)))
mbinned = arrayfun(@(idx) m(indices == idx, :), unique(indices), 'UniformOutput', false)
mcol1 = accumarray(indices, m(:, 1), [], @mean);
mcol2 = accumarray(indices, m(:, 2), [], @mean);
meanbin = [mcol1 mcol2]
댓글 수: 0
추가 답변 (2개)
dpb
2014년 11월 7일
One option -- see
doc histc
댓글 수: 1
Andrei Bobrov
2014년 11월 7일
편집: Andrei Bobrov
2014년 11월 7일
comment by Arne
Thank you :) - I thought of histc as well; but as I said Im rather new to MatLab - so far I only used histc for separating a vector and counting the frequency of certain ranges.
Now, I need to separate for a certain array column and actually obtain the values of both columns not just the counts.
Do you know of a syntax?
E.G.
I have Matrix
1 2
2 5
4 7
5 10
Let's say I'd like to obtain the rows (with actual values) that have column2-value 0-1; 1-2; ... 9-10.
I haven't managed to get to that result with histc. But maybe I have overseen sth..
Arne
2014년 11월 7일
댓글 수: 5
Guillaume
2014년 11월 7일
Ah, 0 is not expected in the indices, since it's not a valid index for a matrix element.
This means that some values are outside the range specified by the bins for histcount. Either you have some values below -430, or more likely, the max is not a multiple of 5,
histcounts(m(:, 2), -430:5:max(m(:, 2))+5)
should solve it.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!