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 个评论

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by