3
$\begingroup$

I have a matrix 300x300 i need to map the values ina greater matrix (900x900) in the positions given by the sequence
$$({3*i-1,3*j-1)}$$
while everything else remain empty, how can achieve this result?

$\endgroup$
4
$\begingroup$

KroneckerProduct

f5 = KroneckerProduct[#, {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}] &;
f6 = KroneckerProduct[SparseArray@#, SparseArray[{2, 2} -> 1, {3, 3}]] &;

SparseArray and Band

f1 = SparseArray[Band[{2, 2}, Automatic, {3, 3}] -> #,3 Dimensions[#]] &;

or

f1 = SparseArray[Band[{2, 2}, 3 Dimensions[#], {3, 3}] -> #] &

ArrayFlatten and ArrayPad

f2 = ArrayFlatten[Map[ArrayPad[{{#}}, 1] &, #, {2}]] &;

Examples

n = 3;
m = RandomInteger[{1, 5}, {n, n}];
Row[MatrixForm /@ {m, f1@m, f2@m}]

Mathematica graphics

Timings:

f0 = (* from bill's answer *) SparseArray[Flatten[{Drop[ArrayRules@ SparseArray[#] /. 
      {x_, y_} -> {3 x - 1, 3 y - 1}, -1], 3 Dimensions[#] -> 0}, 1]] &;

f3 = (* from george2079's answer *) SparseArray[{i_ /; Mod[i + 1, 3] == 0, 
      j_ /; Mod[j + 1, 3] == 0} :> #[[(i + 1)/3, (j + 1)/3]], {3 Length@#, 3 Length@#}] &;

f4 = (* from ciao's answer *) Upsample[#, 3, 2] &;

n = 300; mm = RandomInteger[10, {n, n}];

Equal @@ Through[{f0, f1, f2, f3, f4, f5, f6}[mm]]

True

Grid[SortBy[{HoldForm[#], First@AbsoluteTiming[#[mm]]} & /@ 
     {f0, f1, f2, f3, f4, f5, f6}, Last], Dividers -> All] // Style[#, 16, Bold, "Panel"] &

Mathematica graphics

$\endgroup$
  • $\begingroup$ it works thanks but i fail at understanding the code, i am just a novice. same thing with bill's answer. what does the & and the # means? $\endgroup$ – Alucard May 13 '16 at 19:05
  • $\begingroup$ @Alucard, f1 = SparseArray[Band[{2, 2}, Automatic, {3, 3}] -> #,3 Dimensions[#]] &; is the same as f1[mat_] : = SparseArray[Band[{2, 2}, Automatic, {3, 3}] ->mat,3 Dimensions[mat]] ; see Function (&) and Slot (#) in the docs. $\endgroup$ – kglr May 13 '16 at 19:10
3
$\begingroup$

Perhaps I'm missing something in the question, but why not just:

new=Upsample[old, 3, 2];
$\endgroup$
  • $\begingroup$ god i have no words, i did not know there was this command $\endgroup$ – Alucard May 13 '16 at 22:56
  • $\begingroup$ @Alucard - Developing a full vocabulary of Mathematica functions is probably on the order of developing a decent vocabulary in English - I doubt there are many that know all of the functions off the tops of their heads... $\endgroup$ – ciao May 13 '16 at 23:15
2
$\begingroup$

Here is one way -- represent the original array in sparse form, then replace the indices with the desired ones:

m = RandomInteger[{-5, 5}, {3, 3}];
Normal@SparseArray[Drop[ArrayRules@SparseArray[m] 
            /. {x_, y_} -> {3 x - 1, 3 y - 1}, -1]] // MatrixForm

More generally, and controlling for the ultimate size of the matrix:

n = 3; m = RandomInteger[{1, 5}, {n, n}];
Normal@SparseArray[Flatten[{Drop[ArrayRules@SparseArray[m] 
     /. {x_, y_} -> {3 x - 1, 3 y - 1}, -1], {3 n, 3 n} -> 0}, 1]]
$\endgroup$
  • 2
    $\begingroup$ I think the dimensions of the resulting matrix should be {3 n, 3 n} (not {n^2,n^2}). $\endgroup$ – kglr May 13 '16 at 20:30
  • $\begingroup$ @kglr - Thanks for noticing... just fooled by the simple n=3 case where 3n==n^2! $\endgroup$ – bill s May 14 '16 at 2:28
1
$\begingroup$

A different SparseArray approach:

n = 3
(m = RandomInteger[{1, 5}, {n, n}]) // MatrixForm
big = SparseArray[ 
     {i_ /; Mod[i + 1, 3] == 0, j_ /; Mod[j + 1, 3] == 0} :>
      #[[(i + 1)/3, (j + 1)/3]] , {3 Length@#, 3 Length@#}] &@m;
MatrixForm[big]
$\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.