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

카테고리

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