Search code examples
matlabsparse-matrix

how to aggregate the following in MATLAB?


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.


Solution

  • I believe it is simply

    M = sparse(v, w, ones(size(v)));
    

    Matlab will take the cumulative sum.