Re: Making rectangular matrix

*To*: mathgroup at smc.vnet.net*Subject*: [mg27160] Re: [mg27141] Making rectangular matrix*From*: Tomas Garza <tgarza01 at prodigy.net.mx>*Date*: Thu, 8 Feb 2001 04:40:46 -0500 (EST)*References*: <200102070712.CAA29619@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Let me see if I get you right. Given a bigMat (2-D) and a smallMat (also 2-D), you want to replace the submatrix of bigMat in an arbitrary (not random, I presume) location by smallMat? If such is the case, here is my suggestion. Suppose, for example, that bigMat is 6 x 7: In[1]:= bigMat = Table[Random[Integer, {0, 9}], {6}, {7}] Out[1]= {{7, 0, 7, 2, 5, 4, 6}, {2, 2, 4, 9, 2, 9, 0}, {5, 2, 5, 6, 3, 4, 8}, {0, 3, 9, 9, 8, 0, 0}, {0, 7, 2, 6, 1, 8, 2}, {8, 5, 5, 7, 5, 7, 0}} and that smallMat is 2 x 3: In[2]:= smallMat = Table[Random[Integer, {0, 9}], {2}, {3}] Out[2]= {{3, 1, 2}, {5, 1, 9}} Now you want to replace the submatrix whose upper left position is e.g. row 1 and column 2 with smallMat, so as to get a new bigMat equal to {{7, 3, 1, 2, 5, 4, 6}, {2, 5, 1, 9, 2, 9, 0}, {5, 2, 5, 6, 3, 4, 8}, {0, 3, 9, 9, 8, 0, 0}, {0, 7, 2, 6, 1, 8, 2}, {8, 5, 5, 7, 5, 7, 0}} OK? Then let vert be the upper left position of the block in bigMat where you want to place smallMat. First you must test that, given vert, the size of smallMat fits into bigMat (i.e. vert +Dimensions[smallMat] -1 <= Dimensions[bigMat]), and then proceed to ReplacePart to each element of the block one by one. Define the function rep as follows: In[3]:= rep[x_, {i_, j_}] := ReplacePart[x, smallMat[[i, j]], {vert[[1]]+ i - 1, vert[[2]] + j - 1}] which simply replaces an element in matrix x with the corresponding element of smallMat. Then In[4]:= b = Partition[Flatten[Outer[List, Range[dims[[1]]], Range[dims[[2]]]]], 2] Out[4]= {{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}} In[5]:= Fold[rep, bigMat, b] Out[5]= {{7, 3, 1, 2, 5, 4, 6}, {2, 5, 1, 9, 2, 9, 0}, {5, 2, 5, 6, 3, 4, 8}, {0, 3, 9, 9, 8, 0, 0}, {0, 7, 2, 6, 1, 8, 2}, {8, 5, 5, 7, 5, 7, 0}} Tomas Garza Mexico City ----- Original Message ----- From: "jeevan" <jeevan4 at hotmail.com> To: mathgroup at smc.vnet.net Subject: [mg27160] [mg27141] Making rectangular matrix > How can I make a square or rectangle matrix(2-D) that can be placed > at random locations of a bigger 2-D matrix.Thank you. >

**References**:**Making rectangular matrix***From:*jeevan4@hotmail.com (jeevan)