MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: assignment of matrix elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg17264] Re: [mg17206] assignment of matrix elements
  • From: "Tomas Garza" <tgarza at mail.internet.com.mx>
  • Date: Fri, 30 Apr 1999 02:34:46 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Richard Finley [rfinley at medicine.umsmed.edu] wrote:

> This seems like it should be straightforward but I am too tired to think
> about it anymore:
>
> If you have a matrix say mat = Array[m, {4,4}]
>
> and a list of indices say v = {{1,2},{1,3},{2,3},{3,1}}
> with another list of the same length giving the values of the matrix
> elements at
> those indices: w = {1,5,-2,1}
>
> How do you assign values to the matrix elements with those values...ie
> so
> that m[1,2]=1, m[1,3]=5 etc...
>
> Obviously you can do it individually by simple assignment:
> m{1,2]=1
>
> That won't work for me because there are thousands of elements...the
> techniques I have tried so far
> have not worked...any help is greatly appreciated.

Hi, Richard!

If I understood your problema correctly, then a possible solution is as
follows:

Suppose your matrix is

In[1]:=
mat=Table[Random[Integer,{0,9}],{i,1,4},{j,1,4}]
Out[17]=
{{6,3,7,4},{8,2,4,1},{5,8,7,4},{4,7,1,5}}

and also

In[2]:=
v= {{1,2},{1,3},{2,3},{3,1}};
w= {1,5,-2,1};

Define a function foo which changes individual elements using ReplacePart:

In[3]:=
foo[x_,a_]:=ReplacePart[x,w[[a]],{v[[a,1]],v[[a,2]]}]

and then use Fold to make the changes throughout:

In[4]:=
Fold[foo,mat,Range[Length[w]]]
Out[4]=
{{6,1,5,4},{8,2,-2,1},{1,8,7,4},{4,7,1,5}}

Hope this helps,

Tomas Garza
Mexico City



  • Prev by Date: Re: infuriating Series[] question
  • Next by Date: Re: ListPlotFit
  • Previous by thread: Re: assignment of matrix elements
  • Next by thread: Re: assignment of matrix elements