Re: Assigning elements to a matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg47370] Re: Assigning elements to a matrix
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Wed, 7 Apr 2004 03:16:28 -0400 (EDT)
- Organization: Universitaet Leipzig
- References: <c4u3ko$9lq$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
SetAttributes[fullAssign,HoldFirst]
fullAssign[m_,{i_?Integer,j_?Integer,val_}]:= m[[i,j]]=val
sparseAssign[{i_?Integer,j_?Integer,val_}]:={i,j}->val
say your list is lst and your matrix is m
than
m=Table[0,{n},{n}];
fullAssign[m,#] & /@ lst;
and
m=SparseArray[sparseAssign /@ lst]
will do it.
Regards
Jens
Mark Coleman wrote:
>
> Greetings,
>
> I need to create a series of n x n matrices, in both conventional and
> sparse matrix formats. I have a (large) table of lists, where each list
> is of the form:
>
> {i,j,value}, where {i,j} are the row-column indices and value is the
> corresponding numerical value, i.e., m[[i,j]]=value.
>
> Using this data I need to create an n x n matrix where all the values
> except those specified in my list are 0. I would also like to create a
> similar matrix, this time using Mathematica's built-in SpareMatrix format.
>
> Can anyone suggest a way to do this?
>
> Thanks,
>
> -mark