Re: Re: my previous question on matrix element assignment
- To: mathgroup at smc.vnet.net
- Subject: [mg17274] Re: [mg17237] Re: my previous question on matrix element assignment
- From: "Tomas Garza" <tgarza at mail.internet.com.mx>
- Date: Fri, 30 Apr 1999 02:34:51 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Richard Finley [rfinley at netdoor.com]wrote: >Nothing like posting a question to the group to make you see the answer...I >had asked how to >make assignments to certain matrix elements of (for example) mat = >Array[m,{3,3}] where the indices of the matrix elements to be assigned >are in the list v = {{1,2},{1,3},{2,3},{3,1}} and the values they are to be >assigned are in the list w = {1, 4, 2, 5}, so m[1,2]=1, m[1,3]=4, etc... > >My head is now clear enough to see the answer: > >sol=mat/.Thread[f/@v->w] > >Thanks to anyone who thinks about it anyway and I am interested to see any >other approaches. and later on > Oops! I forgot to mention that f[{a_,b_}] := m[a,b] and the array > should have been written as mat = Array[m, {3,3}] Richard, Quite so, and your solution seems to work fine so long as the array m has not been assigned numerical values. However, if the elements of your original mat are numbers, and some of them are repeated, then the transformation rule f/@v->w is going to produce unwanted results. Try, for example, In[1]:= mat={{2,2,2},{3,3,3},{4,4,4}}; In[2]:= v = {{1,2},{1,3},{2,3},{3,1}}; w = {1, 4, 2, 5}; In[3]:= f[{a_,b_}]:=mat[[a,b]]; In[4]:= sol=mat/.Thread[gr/@v->w] Out[4]= {{1,1,1},{2,2,2},{5,5,5}} whereas the correct solution is {{2,1,4},{3,3,2},{5,4,4}}. Tomas Garza Mexico City