1
$\begingroup$

Minding the fact that

In[102]:= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}[[1 ;; 2, 1 ;; 2]]

Out[102]= {{1, 2}, {4, 5}}

is the upper left submatrix.

I would like to do the following :

> helios = {{{3, 2}, {1, 2}}, {{3, 2}, {2, 1}}, {{3, 1}, {2, 2}}, {{1, 
>     3}, {2, 2}}, {{2, 3}, {1, 2}}, {{2, 3}, {2, 1}}, {{1, 2}, {3, 
>     2}}, {{2, 2}, {3, 1}}, {{2, 1}, {3, 2}}, {{2, 1}, {2, 3}}, {{1, 
>     2}, {2, 3}}, {{2, 2}, {1, 3}}}

ReplacePart[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {1 ;; 2, 
     1 ;; 2} -> #] & /@ helios

That is, i would like to replace the upper left submatrix by a series of other matrices ! But this does not work as :

ReplacePart::pkspec1: The expression 1;;2 cannot be used as a part specification.

Does anyone have a efficient way of doing this ? with built-in functions if possible.

Thank you very much.

$\endgroup$
3
$\begingroup$

This is straight-forward to do with assignment:

m = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
m[[1 ;; 2, 1 ;; 2]] = {{3, 2}, {1, 2}};
m // MatrixForm

Output

We can get the list of all matrices like this:

newMatrices = ConstantArray[m, Length[helios]];
newMatrices[[All, 1 ;; 2, 1 ;; 2]] = helios;
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.