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:
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: 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: 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: 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

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