Splitting an array into smaller unequal-sized arrays dependend on array-column values

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
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.
Arne
Arne 2014 年 11 月 7 日
just the average values thankfully

サインインしてコメントする。

 採用された回答

Guillaume
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]

その他の回答 (2 件)

dpb
dpb 2014 年 11 月 7 日
One option -- see
doc histc

1 件のコメント

Andrei Bobrov
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
Arne 2014 年 11 月 7 日

0 投票

your code looks very elegant and makes sense; however, due to the properties of my array I get the following error when trying to type the mcol-lines: Error using accumarray First argument SUBS must contain positive integer subscript. - maybe since my columnelements are negative and double numbers it has some difficulties.
Secondly, in the next line mcol2 (...) - do we have to fill in again m(:, 1) or rather m(:, 2)
sorry for the questions - getting there :)

5 件のコメント

Guillaume
Guillaume 2014 年 11 月 7 日
Please use, the 'Comment on this Answer' button rather than starting a new answer. It's easier to follow the discussion.
If indices (the SUBS of accumarray) is the third output of histcounts (or 2nd of histc) there's no way it can contain non-positive or non-integer values as it's a vector of matrix indices. You must have done something different.
Yes, sorry I made a typo for the 2nd accumarray. It should indeed be column 2. I'll edit the answer.
Arne
Arne 2014 年 11 月 7 日
編集済み: Arne 2014 年 11 月 7 日
sorry for that. yes it's kind of strange; the indices-vector is just as I expected (its values go from 0 on to positive integer values) - nothing unusual. still i obtain the error message. I even copy-pasted your code- with your example everything works fine, however with my matrix there is again the SUBS-message. The only thing I changed from the copy pasted code were the borders in the second line: I set them like this: [...] histcounts(m(:,2), -430:5:max(m(:, 2))) nothing to unsual here either - also the code works fine at this line. But even with your border-settings my raw data has problems in the mcol-lines..
I simply imported it as usual matrix (though its a large one 31100 x 2 double). Maybe the MatLab script has problems with that
dpb
dpb 2014 年 11 月 7 日
... the indices-vector is just as I expected (its values go from 0 on to positive integer values) - nothing unusual.
But it IS unusual--actually it's more than "unusual" it's wrong. Matlab arrays are (and ever have been and ever shall be, it appears) 1-, not 0-based.
Your indices vector MUST go from 1:N, not 0:N-1
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.
Arne
Arne 2014 年 11 月 7 日
yes, that was a rookie mistake.. but it works now - so happy; saved me at least 3 days of work - thank you, guys!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2014 年 11 月 7 日

コメント済み:

2014 年 11 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by