Matt,
I think it works. Very nice.
Thanks
Jovan
"matt dash" wrote in message <l6dg4s$c14$1@newscl01ah.mathworks.com>...
> "matt dash" wrote in message <l6dfpe$5m2$1@newscl01ah.mathworks.com>...
> > "Eric Sampson" wrote in message <l6db5d$1f6$1@newscl01ah.mathworks.com>...
> > > "Jovan" wrote in message <l6d9vp$8mo$1@newscl01ah.mathworks.com>...
> > > >
> > > > I have three arrays i, j, and val. i and j are matrix coordinates and val is the
> > > > value at that coordinate. The same (i,j) with the same or different val can
> > > > occur multiple times. For example:
> > > >
> > > > (i,j,val) = 1,2,3.14
> > > > (i,j,val) = 1,2,3.14
> > > > (i,j,val) = 1,2,3.14
> > > >
> > > > I need to form a sparse matrix from the above arrays so that multiple values
> > > > at the same coordinate are accumulated. In the above example, I'd have
> > > > 3.14 + 3.14 + 3.14 at the matrix location (1,2).
> > > >
> > > > How do I vectorize forming a sparse matrix from the described arrays. Looping
> > > > in R2013a is still between 10 and 15 times slower than vectorizing.
> > > >
> > > > Thanks
> > >
> > > I believe it should be as simple as "S = sparse(i,j,val);"
> >
> > You can use accumarray to handle the summation, but it will return a full matrix. If memory isnt an issue you can then just wrap this with sparse(). Otherwise I think you'd need to call accumarray using dummy indices first so that the resulting full matrix is small, then call sparse using the syntax above with the real indices to expand it out to full size.
>
> Wait, scratch that, you can specify that accumarray should return a sparse matrix.
>
> I think A=accumarray([i j],val,[],@sum,0,true) will do the trick.
