How to a find the frequency of a point or values?

3 ビュー (過去 30 日間)
John Alperto
John Alperto 2015 年 4 月 6 日
回答済み: Star Strider 2015 年 4 月 6 日
If I have a Nx2 vector (in this case, N=20): A =
0 1
0 1
1 0
0 -1
0 -1
-1 0
-1 0
0 1
0 1
1 0
0 -1
0 -1
-1 0
-1 0
0 1
-1 0
1 0
0 -1
0 -1
-1 0
I want to know how many times each unique point (-1,0), (1,0), (0,1), and (0, -1) appears in this vector. For example, the point (-1,0) occurs 6 times. Is there a fast, simple way to do this? Using the find function or a for loop were too cumbersome I thought, but I couldn't find a simple function that would help with this. Is anyone aware of a function, or a simple method to accomplish this? Thanks

回答 (2 件)

Mahdiyar
Mahdiyar 2015 年 4 月 6 日
Hi
you can download the matlab function named findsubmat from the following link. Then use it for each pair. For example:
B1 = findsubmat(A, [-1 0]);
B2 = findsubmat(A, [1 0]);
B3 = findsubmat(A, [0 1]);
B4 = findsubmat(A, [0 -1]);
Regards,
  3 件のコメント
Mahdiyar
Mahdiyar 2015 年 4 月 6 日
編集済み: Mahdiyar 2015 年 4 月 6 日
Please follow the code below:
A =[ 0 1
0 1
1 0
0 -1
0 -1
-1 0
-1 0
0 1
0 1
1 0
0 -1
0 -1
-1 0
-1 0
0 1
-1 0
1 0
0 -1
0 -1
-1 0];
D1 = 0; D2 = 0; D3 = 0; D4 =0;
for i=1:size(A,1)
B = A(i,:);
C = B - [-1 0];
NZ = length(find(C==0));
if NZ == 2
D1 = D1 +1;
end
C = B - [1 0];
NZ = length(find(C==0));
if NZ == 2
D2 = D2 +1;
end
C = B - [0 1];
NZ = length(find(C==0));
if NZ == 2
D3 = D3 +1;
end
C = B - [0 -1];
NZ = length(find(C==0));
if NZ == 2
D4 = D4 +1;
end
end
[D1 D2 D3 D4]
John Alperto
John Alperto 2015 年 4 月 6 日
Thanks.

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


Star Strider
Star Strider 2015 年 4 月 6 日
This works:
A = [0 1
0 1
1 0
0 -1
0 -1
-1 0
-1 0
0 1
0 1
1 0
0 -1
0 -1
-1 0
-1 0
0 1
-1 0
1 0
0 -1
0 -1
-1 0];
[Au,~,ic] = unique(A,'rows');
k = accumarray(ic,1);
fprintf('\n\tPoints\tFrequency\n')
fprintf('\t%2d %2d\t\t%2d\n', [Au k]')
and produces:
Points Frequency
-1 0 6
0 -1 6
0 1 5
1 0 3

カテゴリ

Help Center および File ExchangeCommunications Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by