Re: my previous question on matrix element assignment
- To: mathgroup at smc.vnet.net
- Subject: [mg17333] Re: my previous question on matrix element assignment
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Fri, 30 Apr 1999 23:22:45 -0400
- References: <7g102c$ecl@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Richard, Here are some approaches - the first one is the most general, the second one may be the same as yours. arr = Array[m, {3, 3}]; mat = arr; Fold[ReplacePart[#1, Sequence @@ #2] &, mat, Transpose[{w, v}]] {{m[1, 1], 1, 4}, {m[2, 1], m[2, 2], 2}, {5, m[3, 2], m[3, 3]}} mat = arr; mat /. Thread[Apply[m, v, {1}] -> w] {{m[1, 1], 1, 4}, {m[2, 1], m[2, 2], 2}, {5, m[3, 2], m[3, 3]}} mat = arr; MapThread[(mat[[Sequence @@ #1]] = #2) &, {v, w}]; mat {{m[1, 1], 1, 4}, {m[2, 1], m[2, 2], 2}, {5, m[3, 2], m[3, 3]}} Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 Richard Finley <rfinley at netdoor.com> wrote in message news:7g102c$ecl at smc.vnet.net... > > 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[mat,{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. > > Richard Finley >