Reputation: 4721
I have two vectors, v
and w
, in MATLAB, both of the same length.
I want to create a sparse matrix A
of size max(v) x max(w)
such that A(i,j)
equals the number of times the pair [i,j]
appears in v
and w
.
Basically, it is something very similar to A(v,w) = 1
.
This would be correct if there were no repetitions, i.e. if there is no pair (i,j)
that appears more than once together in v
and w
.
But I do have repetitions, and I am not sure how to elegantly accommodate for them.
Note: v
and w
are very long. The values they have are about 100 smaller than their length.
Upvotes: 1
Views: 192
Reputation: 46375
I believe it is simply
M = sparse(v, w, ones(size(v)));
Matlab will take the cumulative sum.
Upvotes: 2