Re: Setting parts of matrices
- Subject: [mg2966] Re: Setting parts of matrices
- From: espen.haslund at fys.uio.no (Espen Haslund)
- Date: 17 Jan 1996 05:07:40 -0600
- Approved: usenet@wri.com
- Distribution: local
- Newsgroups: wri.mathgroup
- Organization: Wolfram Research, Inc.
- Sender: mj at wri.com
In article <4cqk0q$ig5 at dragonfly.wri.com>, jnygaard at math.uio.no says...
>
>Can somebody tell me how to set a submatrix of a larger matrix?
>Assume a is a larger matrix than b. I would like to do something
>like
>
> a[[Range[i1,i2],Range[j1,j2]]]=b;
>
>Unfortunately, this does not work, and the Mathematica-book of
>Wolfram doesn't tell me how to do it. It just says that
>
> a[[Range...
>
>is the way to *extract* things from a matrix.
>
>
>Jens Olav Nygaard
>
>
Hei, Jens,
It appears that no elegant solutions are posted for your problem,
so here is an un-elegant one:
replaceMatrixPart[a_,i1_,j1_,b_] :=
Module[{i2, j2, aa},
aa = a;
{i2, j2} = {i1, j1} + Dimensions[b] - 1;
Do[aa[[i, j]] = b[[i-i1+1, j-j1+1]], {i,i1,i2}, {j,j1,j2}];
aa
]
This replaces part of the matrix a [from (i1, j1) to the extent of b]
with the elements of the matrix b,
(note that the original matrix a is kept intact)
This Function is NOT tested for robustness or correctness!
I just made it.
-Espen