Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how do I vectorize this?

Subject: how do I vectorize this?

From: Jovan

Date: 18 Nov, 2013 14:59:05

Message: 1 of 5


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

Subject: how do I vectorize this?

From: Eric Sampson

Date: 18 Nov, 2013 15:19:09

Message: 2 of 5

"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);"

Subject: how do I vectorize this?

From: matt dash

Date: 18 Nov, 2013 16:38:06

Message: 3 of 5

"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.

Subject: how do I vectorize this?

From: matt dash

Date: 18 Nov, 2013 16:44:12

Message: 4 of 5

"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.

Subject: how do I vectorize this?

From: Jovan

Date: 18 Nov, 2013 21:10:08

Message: 5 of 5


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.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us