I have a matrix m1
of size $3 \times 19$.
Rows $1$, $2$ and $3$ represent $3$ different groups, and columns represent $4$ different blocks:
- block1 - columns $1$, $2$, and $3$,
- block2 - columns $4$, $5$, $6$, $7$, and $8$,
- block3 - columns $9$, $10$, $11$, and $12$,
- block4 - columns $13$--$19$.
I want to construct a SparseArray
, of the size $3 \times 19$, such that if any of the matrix m1
elements is less than for example $5000$, then all of the elements of the corresponding group and block should be equal to $1$. If all of the elements of the corresponding group and block are greater than $5000$, then the values of the SparseArray
should be equal to $0$.
This is my input matrix m1
:
m1 = {{17000, 14542, 17000, 7000, 7000, 7000, 7000, 5666, 5127, 3810,
6027, 7000, 12000, 12000, 12000, 12000, 12000, 17000,
12000}, {17000, 12070, 17000, 7000, 7000, 7000, 7000, 5100, 4435,
3010, 5575, 7000, 12000, 12000, 12000, 12000, 12000, 17000,
12000}, {17000, 9743, 17000, 7000, 7000, 7000, 5530, 4250, 4358,
2876, 5002, 7000, 12000, 12000, 12000, 12000, 12000, 17000, 12000}}
I use Map
and Boole
to generate matrix m2
:
m2 = Boole[Map[# < 5000 &, m1, {2}]]
This is the output for m2
:
(* {{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}} *)
Then I need to specify the conditions for my final solution.
solution=Normal[SparseArray[{
{i_, j_} /; i == 1 && 1 <= j <= 3 && Total[m2[[1, 1 ;; 3]]] >= 1 -> 1,
{i_, j_} /; i == 1 && 4 <= j <= 8 && Total[m2[[1, 4 ;; 8]]] >= 1 -> 1,
{i_, j_} /; i == 1 && 9 <= j <= 12 && Total[m2[[1, 9 ;; 12]]] >= 1 -> 1,
{i_, j_} /; i == 1 && 13 <= j <= 19 && Total[m2[[1, 13 ;; 19]]] >= 1 -> 1,
{i_, j_} /; i == 2 && 1 <= j <= 3 && Total[m2[[2, 1 ;; 3]]] >= 1 -> 1,
{i_, j_} /; i == 2 && 4 <= j <= 8 && Total[m2[[2, 4 ;; 8]]] >= 1 -> 1,
{i_, j_} /; i == 2 && 9 <= j <= 12 && Total[m2[[2, 9 ;; 12]]] >= 1 -> 1,
{i_, j_} /; i == 2 && 13 <= j <= 19 && Total[m2[[2, 13 ;; 19]]] >= 1 -> 1,
{i_, j_} /; i == 3 && 1 <= j <= 3 && Total[m2[[3, 1 ;; 3]]] >= 1 -> 1,
{i_, j_} /; i == 3 && 4 <= j <= 8 && Total[m2[[3, 4 ;; 8]]] >= 1 -> 1,
{i_, j_} /; i == 3 && 9 <= j <= 12 && Total[m2[[3, 9 ;; 12]]] >= 1 -> 1,
{i_, j_} /; i == 3 && 13 <= j <= 19 && Total[m2[[3, 13 ;; 19]]] >= 1 -> 1,
Dimensions[m2]]]
This is the result I wanted:
(* {{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}} *)
I believe that there is a better way to get the results I need.