How do I count the number of matching pairs in 2 vectors and use that as colors in a contour plot?

18 ビュー (過去 30 日間)
I have 2 vectors A and B. Both are column vectors and both contain 1000 values.
For example:
A=[1,2,5,1,6,2,8,2,9];
B=[3,4,2,3,7,4,5,4,8];
I wish to find out how many times that (A,B) match so that I can create a contour plot of (A,B) which is colour coded in accordance to the number of matching occurrences:
e.g. (1,3) = 2 occurrences (2,4) = 3 occurrences (5,2) = 1 Occurrence ....etc
I know that I need to create a for loop and I was hoping the unique command would work but as yet I have been unable to generate a working code.

採用された回答

Walter Roberson
Walter Roberson 2011 年 3 月 30 日
accumarray([A(:), B(:)])
Provided that the values are positive integers. Otherwise,
[bA, mA, nA] = unique(A);
[bB, mB, nB] = unique(B);
accumarray([nA(:), nB(:)])
Then entry (I,J) counts the match bA(I) to bB(J)
  4 件のコメント
Jennifer
Jennifer 2011 年 3 月 31 日
Brilliant - thank you.
This works with the example I gave above, however with my actual vectors (more than 1500 values) I get the following error message:
??? Error using ==> accumarray
First input SUBS must contain positive integer subscripts.
I have tried rounding the values and also a few other tricks and can't seem to get it to work. It is rather frustrating.
Do you know what might be causing the problem? I can send you the vectors if needed.
Any advice welcome and thanks in advance
Teja Muppirala
Teja Muppirala 2011 年 3 月 31 日
What Walter wrote should work. You have to feed in nA and nB into accumarray. Are you sure you wrote it correctly?
[bA, mA, nA] = unique(A);
[bB, mB, nB] = unique(B);
accumarray([nA(:), nB(:)],1)

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

その他の回答 (2 件)

Todd Flanagan
Todd Flanagan 2011 年 3 月 31 日
A = [1,2,5,1,6,2,8,2,9];
B = [3,4,2,3,7,4,5,4,8];
c = accumarray([A(:), B(:)])
c =
0 0 2 0 0 0 0 0
0 0 0 3 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1
looking at unique rows
pairs = unique([A(:), B(:)], 'rows')
pairs =
1 3
2 4
5 2
6 7
8 5
9 8
To find the count for row 1
>> c(1,3)
ans =
2
If your rows don't contain positive integers, you can still do the problem by converting it to indeces using unique.
Unique can create a vector of the unique elements and also a vector of indeces to reconstruct the original vector based on these values. For example:
>> [bA, mA, nA] = unique(A)
bA =
1 2 5 6 8 9
mA =
4 8 3 5 7 9
nA =
1 2 3 1 4 2 5 2 6
bA(nA) gives you back the original vector but lets you operate in terms of something (indeces) that ensure you are using positive integers.
>> bA(nA)
ans =
1 2 5 1 6 2 8 2 9
Now to use accumarray:
[bA, mA, nA] = unique(A);
[bB, mB, nB] = unique(B);
accumarray([nA(:), nB(:)],1)
ans =
0 2 0 0 0 0
0 0 3 0 0 0
1 0 0 0 0 0
0 0 0 0 1 0
0 0 0 1 0 0
0 0 0 0 0 1
To find the rows again,
your pairs are now in terms of indeces:
>> pairs = unique([nA(:) nB(:)], 'rows')
pairs =
1 2
2 3
3 1
4 5
5 4
6 6
So, c(1,2) = 2
and the original pair is
>> [bA(1) bB(2)]
ans =
1 3
  4 件のコメント
Carlos Goncalves Moreira
Carlos Goncalves Moreira 2018 年 1 月 11 日
Hi Jennifer,
Did you find a working code to plot these results?
Thanks a lot
Hassan
Hassan 2018 年 3 月 15 日
hi, you can use digraph
G=digraph(accumarray([nA(:), nB(:)],1));
Best, HM

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


Hassan
Hassan 2018 年 3 月 15 日
Hi all, The suggested method accumarray([nA(:), nB(:)],1) is good to have counts of pairs in 2D representation. What if I interested in a square matrix, I mean full representation of length n by n?
let's say I have two vectors x=1:10, and y=1:8. accumarray([nA(:), nB(:)],1) will result in 10 by 8 matrix. How can I make the result 10 by 10 with two lines of zeros?
Thanks a lot, HM
  1 件のコメント
Hassan
Hassan 2018 年 3 月 15 日
I found the answer... just add a dimension argument next to the 1
accumarray([nA(:), nB(:)],1,[10 10])

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by