How do I count the number of matching pairs in 2 vectors and use that as colors in a contour plot?
조회 수: 9 (최근 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.
댓글 수: 0
채택된 답변
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
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
2011년 3월 31일
A = [1,2,5,1,6,2,8,2,9];
B = [3,4,2,3,7,4,5,4,8];
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
2018년 1월 11일
Hi Jennifer,
Did you find a working code to plot these results?
Thanks a lot
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
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 Exchange에서 Contour Plots에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!