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

18 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 30 Mär. 2011
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 Kommentare
Jennifer
Jennifer am 31 Mär. 2011
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 am 31 Mär. 2011
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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Todd Flanagan
Todd Flanagan am 31 Mär. 2011
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 Kommentare
Hassan
Hassan am 15 Mär. 2018
hi, you can use digraph
G=digraph(accumarray([nA(:), nB(:)],1));
Best, HM

Melden Sie sich an, um zu kommentieren.


Hassan
Hassan am 15 Mär. 2018
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

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by