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?
$\begingroup$
$\endgroup$
$\begingroup$
$\endgroup$
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}]
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"] &
-
$\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 asf1[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
$\begingroup$
$\endgroup$
Perhaps I'm missing something in the question, but why not just:
new=Upsample[old, 3, 2];
-
$\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
$\begingroup$
$\endgroup$
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]]
$\begingroup$
$\endgroup$
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]