2
$\begingroup$

I would like to generate a $3D$ matrix of dimension $(n1\times n1\times n1)$ compose of $216$ matrix blocks $(n2\times n2\times n2)$. All 216 matrix blocks are subdivided into 4 groups, each one with 54 identical matrices. All matrix elements of each matrix block taking into account each group have the same value, which can be $a, b, c$ or $d$. The position of each block is randomly allocated in the main matrix $(n1\times n1\times n1)$. Figure below represents the proposal.

enter image description here

Taking into account that the sum of all blocks' volume does not exactly correspond to the volume of the main matrix $(n1\times n1\times n1)$ and the difficulty in adjusting the blocks in the main matrix, null elements can be used to complete the elements in the main matrix.

Below, is a fragment of the code:

n1 = 100;(*dimension of main matrix*)
n2 = 16;(*dimension of block matrix*)
a = 2; b = 3; c = 5; d = 7; (*types of elements contained in each matrix block*)
m = SparseArray[{{i_, j_} -> 0}, {n1, n1, n1}]; (*initial main matrix with null elements*)
m1 = SparseArray[{{i_, j_} -> a}, {n2, n2, n2}]; (*first type of matrix block*)
m2 = SparseArray[{{i_, j_} -> b}, {n2, n2, n2}]; (*second type of matrix block*)
m3 = SparseArray[{{i_, j_} -> c}, {n2, n2, n2}]; (*third type of matrix block*)
m4 = SparseArray[{{i_, j_} -> d}, {n2, n2, n2}]; (*fourth type of matrix block*)
res= ArrayFlatten[];

The idea would be to generate 216 matrix blocks subdivide into four groups of 54 matrices of $m1$, $m2$, $m3$, and $m4$ and allocate them in random positions in the main matrix $m$.

How to do this?

Can anybody help me?

Thank you so much in advance.

$\endgroup$
1

1 Answer 1

1
$\begingroup$

Maybe this is what you want. This does not use any SparseArrays because the resulting array is just not sparse enough for SparseArray providing any benefit.

k = 6;
A = ArrayReshape[
   Join[
     ConstantArray[a, Quotient[k^3, 4]],
     ConstantArray[b, Quotient[k^3, 4]],
     ConstantArray[c, Quotient[k^3, 4]],
     ConstantArray[d, Quotient[k^3, 4]]
     ][[PermutationList[RandomPermutation[k^3], k^3]]],
   {k, k, k}
   ];
ones = ConstantArray[1, ConstantArray[n2, 3]];
B = ArrayPad[KroneckerProduct[A, ones], ConstantArray[{0, n1 - n2 k}, 3]];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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