Re: copying values of entries between matrices
- To: mathgroup at smc.vnet.net
- Subject: [mg77984] Re: copying values of entries between matrices
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 21 Jun 2007 05:32:39 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f5atd0$an9$1@smc.vnet.net>
text.tex wrote: > Hi, > here is my problem: > I have a 18x18 matrix called entMatr. > I want to copy the values of the elements of the lower left half of > entMatr into a 171x1 matrix called dOneCol. > I had tried a 3-nested loop for copying the values from entMatr to > dOneCol; the current version where I tried to replace the loop over > the rows of dOneCol with a counter q produces a result, however the > result is not what I want... > > I'd greatly appreciate any pointers how to get this working! > Thanks, > Claus > > This is what I tried: > > <pre> > (* initialize entMatr *) > entMatr = Table[Subscript[m, i, j], {i, 18}, {j, 18}]; > > (* fill the lower left half of entMatr with some values *) > For[i = 1, i < 18 + 1, i++, > For[j = i, j < 18 + 1, j++, > entMatr[[j, i]] = i > ] > ] > > (* initialize dOneCol *) > dOneCol = Table[Subscript[n, x], {x, 171}]; > > (* attempt to copy the values from entMatr to dOneCol *) > q = 0; > For[k = 1, k < 18 + 1, k++, > For[p = k, p < 18 + 1, p++, > dOneCol[[q]] = entMatr[[p,k]] > q++ > ] > ] > dOneCol > </pre> Assuming I have correctly understood what you wanted, one way of doing that is the following: dOneCol = Reap[Do[If[j <= i, Sow[entMatr[[i, j]]]], {i, 18}, {j, 18}]][[2, 1]] For instance, entMatr = Array[Subscript[m, ##1] & , {18, 18}]; For[i = 1, i < 18 + 1, i++, For[j = i, j < 18 + 1, j++, entMatr[[j, i]] = i]] MatrixForm[entMatr] dOneCol = Reap[Do[If[j <= i, Sow[entMatr[[i, j]]]], {i, 18}, {j, 18}]][[ 2, 1]] Length[dOneCol] MatrixForm[dOneCol] Regards, Jean-Marc