How can I count the number of elements of a given value in a matrix?

2 ビュー (過去 30 日間)
MIHYUN
MIHYUN 2015 年 3 月 5 日
編集済み: Guillaume 2015 年 3 月 5 日
I have a matrix such as.
44.147231 ; 24.358619
44.461415 ; 24.118248
44.258173 ; 23.185014
43.166729 ; 24.100443
43.159002 ; 26.708013
42.954059 ; 27.338612
44.139759 ; 23.927882
44.424679 ; 25.301393
42.957588 ; 25.530434
44.615223 ; 24.200719
44.384409 ; 25.486735
43.965701 ; 28.454219
43.400971 ; 21.492123
42.068687 ; 22.303336
43.245372 ; 28.932286
44.593751 ; 23.635179
The first column is longitude , the second column is latitude.
So, I want to count the number of region.
In the above matrix,
But I don't know how make the code.
Please help me...

採用された回答

Guillaume
Guillaume 2015 年 3 月 5 日
Note: when posting examples, it' s much easier for us if you use proper matlab syntax. As it is, I had to paste your example in a text editor to remove the ; between the numbers.
As often the case in matlab, the most efficient solution is to avoid loops. The simplest way to solve your problem is to convert your latitude and longitude into matrix coordinates (by rounding down the numbers, subtracting the min of each column, and adding 1) and use these coordinates with accumarray to compute the histogram:
%this can be pasted straight into matlab, your example can't:
longlat = [44.147231 24.358619
44.461415 24.118248
44.258173 23.185014
43.166729 24.100443
43.159002 26.708013
42.954059 27.338612
44.139759 23.927882
44.424679 25.301393
42.957588 25.530434
44.615223 24.200719
44.384409 25.486735
43.965701 28.454219
43.400971 21.492123
42.068687 22.303336
43.245372 28.932286
44.593751 23.635179];
gridcoord = floor(longlat); %get integer by rounding down
gridcoord = 1 + bsxfun(@minus, gridcoord, min(gridcoord)); %shift everything so offsets start at 1
gridpop = accumarray(gridcoord, 1)
gridpop is the population of each region. Note that longitude is the vertical coordinate, increasing down and latitude is the horizontal coordinate, increasing right. Like matrices in matlab. If you want it in the same format as in your example:
gridpop = flipud(gridpop')
  1 件のコメント
MIHYUN
MIHYUN 2015 年 3 月 5 日
Thank you for helping me!!
It is now possible to reduce the time thanks to you.

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

その他の回答 (3 件)

Yona
Yona 2015 年 3 月 5 日
first use floor to round it.
after this run double loop, one on latitude and the second on longitude, and by using length(find(A(:,1)==i & A(:,2)==j)) you will get the number of each combination.

puneet joshi
puneet joshi 2015 年 3 月 5 日
well u can compare the complete matrix with the num u want to count .... and each time there is a match u can increase the count

Andrei Bobrov
Andrei Bobrov 2015 年 3 月 5 日
編集済み: Andrei Bobrov 2015 年 3 月 5 日
n = floor(min(a));
m = ceil(max(a));
[~,~,lo] = histcounts(a(:,1),n(1):m(1));
[~,~,la] = histcounts(a(:,2),n(2):m(2));
out = flip(accumarray([la,lo],1),1)
  2 件のコメント
Jos (10584)
Jos (10584) 2015 年 3 月 5 日
Did you mean
[~, lo] = histc(..)
Guillaume
Guillaume 2015 年 3 月 5 日
編集済み: Guillaume 2015 年 3 月 5 日
histc has been deprecated in R2014b. The replacement is histcounts which uses a different syntax and of course behaves slightly differently while still not solving all the issues of histc.
The syntax for histcounts is indeed the one Andrei used in his answer.

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

Community Treasure Hunt

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

Start Hunting!

Translated by