Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: vectorization in assignment

Subject: vectorization in assignment

From: Greg von Winckel

Date: 22 Jun, 2008 11:03:06

Message: 1 of 4

Suppose I have a large sparse matrix A and I would like to
copy a small matrix M into various portions of A. One way to
do this would be

for j=1:N

A(a(j),b(j))=M;

end

where a and b are anonymous functions which give a set of
indices for each j. Is there a way to do this assignment
without a loop, for example with arrayfun and cell2mat?

Thanks for your help!

Greg

Subject: Re: vectorization in assignment

From: John D'Errico

Date: 22 Jun, 2008 14:36:02

Message: 2 of 4

"Greg von Winckel" <gregvw@gmail.com> wrote in message
<g3lbha$iq5$1@fred.mathworks.com>...
> Suppose I have a large sparse matrix A and I would like to
> copy a small matrix M into various portions of A. One way to
> do this would be
>
> for j=1:N
>
> A(a(j),b(j))=M;
>
> end
>
> where a and b are anonymous functions which give a set of
> indices for each j. Is there a way to do this assignment
> without a loop, for example with arrayfun and cell2mat?
>
> Thanks for your help!
>
> Greg

Do it in ONE call to sparse, at the end.

Compile a list of row and column indices for
each element. I'd build it asa cell array of nx3
arrays, where the first column is row index,
the second column is column index, and the
third column the value at that position.

At the end, use cell2mat to combine it into
one flat array, then and only then do you
call sparse.

John

Subject: Re: vectorization in assignment

From: Greg von Winckel

Date: 23 Jun, 2008 08:27:01

Message: 3 of 4

Got it! Thanks, John!


"John D'Errico" <woodchips@rochester.rr.com> wrote in
message <g3lo0i$9gj$1@fred.mathworks.com>...
> "Greg von Winckel" <gregvw@gmail.com> wrote in message
> <g3lbha$iq5$1@fred.mathworks.com>...
> > Suppose I have a large sparse matrix A and I would like to
> > copy a small matrix M into various portions of A. One way to
> > do this would be
> >
> > for j=1:N
> >
> > A(a(j),b(j))=M;
> >
> > end
> >
> > where a and b are anonymous functions which give a set of
> > indices for each j. Is there a way to do this assignment
> > without a loop, for example with arrayfun and cell2mat?
> >
> > Thanks for your help!
> >
> > Greg
>
> Do it in ONE call to sparse, at the end.
>
> Compile a list of row and column indices for
> each element. I'd build it asa cell array of nx3
> arrays, where the first column is row index,
> the second column is column index, and the
> third column the value at that position.
>
> At the end, use cell2mat to combine it into
> one flat array, then and only then do you
> call sparse.
>
> John

Subject: Re: vectorization in assignment

From: Greg von Winckel

Date: 24 Jun, 2008 18:58:01

Message: 4 of 4

In case you'd like to see the implementation:

rowx=stack(cell2mat(arrayfun(@(j)
repmat((j-1)*Nq+1:j*Nq',1,Nq),1:Nxe,'UniformOutput',false)));
colx=stack(cell2mat(arrayfun(@(j)
repmat((j-1)*Ni-(j-2):j*(Ni-1)+1,Nq,1),1:Nxe,'UniformOutput',false)));

px=sparse(rowx,colx,repmat(p(:),Nxe,1));

This is for a nodal hp-spectral element code where I wish to
build global basis functions. Nq is the number of quadrature
points per element, Ni, is the number of basis functions,
Nxe is the number of elements.

stack=@(A) A(:);
 
Works like a charm.

Thanks again, John.








"John D'Errico" <woodchips@rochester.rr.com> wrote in
message <g3lo0i$9gj$1@fred.mathworks.com>...
> "Greg von Winckel" <gregvw@gmail.com> wrote in message
> <g3lbha$iq5$1@fred.mathworks.com>...
> > Suppose I have a large sparse matrix A and I would like to
> > copy a small matrix M into various portions of A. One way to
> > do this would be
> >
> > for j=1:N
> >
> > A(a(j),b(j))=M;
> >
> > end
> >
> > where a and b are anonymous functions which give a set of
> > indices for each j. Is there a way to do this assignment
> > without a loop, for example with arrayfun and cell2mat?
> >
> > Thanks for your help!
> >
> > Greg
>
> Do it in ONE call to sparse, at the end.
>
> Compile a list of row and column indices for
> each element. I'd build it asa cell array of nx3
> arrays, where the first column is row index,
> the second column is column index, and the
> third column the value at that position.
>
> At the end, use cell2mat to combine it into
> one flat array, then and only then do you
> call sparse.
>
> John

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
vectorization Greg von Winckel 22 Jun, 2008 07:05:06
sparse matrix Greg von Winckel 22 Jun, 2008 07:05:06
assignment Greg von Winckel 22 Jun, 2008 07:05:06
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics