In case you'd like to see the implementation:
rowx=stack(cell2mat(arrayfun(@(j)
repmat((j1)*Nq+1:j*Nq',1,Nq),1:Nxe,'UniformOutput',false)));
colx=stack(cell2mat(arrayfun(@(j)
repmat((j1)*Ni(j2):j*(Ni1)+1,Nq,1),1:Nxe,'UniformOutput',false)));
px=sparse(rowx,colx,repmat(p(:),Nxe,1));
This is for a nodal hpspectral 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
