# Mapping over array/matrix the efficient(compact) way

Update

In short, I have myCAans[[1 ;; 5]] like this:

{{0, 0, 0, True, 0, 0, 0, 0, True, 0, "T0", 39}, {0, 0, 0, True, 0, 0, 0, 1, True, 1, "B", 101500}, {0, 0, 0, True, 0, 0, 0, 2, True, 2, "B", 100}, {0, 0, 0, True, 0, 0, 0, 3, True, 3, "B", 1000}, {0, 0, 0, True, 0, 0, 0, 4, True, 4, "B", 1000}}

Dimensions[myCAans]


{3000, 12}

Each line of

Total[ Select[myCAans , #[[5]] == 0 && #[[10]] == 0 &][[;; , 12]] ]
Total[ Select[myCAans , #[[5]] == 0 && #[[10]] == 1 &][[;; , 12]] ]
Total[ Select[myCAans , #[[5]] == 0 && #[[10]] == 2 &][[;; , 12]] ]
Total[ Select[myCAans , #[[5]] == 1 && #[[10]] == 0 &][[;; , 12]] ]
Total[ Select[myCAans , #[[5]] == 5 && #[[10]] == 6 &][[;; , 12]] ]


works fine.

I would like to know how to efficiently find

Total[ Select[myCAans , #[[5]] == i && #[[10]] == j &][[;; , 12]] ]


for i from 0 to 3 and j from 0 to 3 for example. Something like

Total[ Select[myCAans , #[[5]] == i && #[[10]] == j &][[;; , 12]] ] -> XXXXXX  <- Table[{i, j}, {i, 0, 3}, {j, 0, 3}]


OP

On a related question here, I am struggling to get my head around for this:

f@Table[{i, j}, {i, 0, 3}, {j, 0, 3}]
f @@ Table[{i, j}, {i, 0, 3}, {j, 0, 3}]
f @@@ Table[{i, j}, {i, 0, 3}, {j, 0, 3}]
f /@ Table[{i, j}, {i, 0, 3}, {j, 0, 3}]

Map[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}], {2, 2}]

MapAt[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}], {1}] // TableForm
MapAt[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}], {2, 1}] // TableForm
MapAt[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}], {2, 2}] // TableForm
MapAt[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}], {2, 3}] // TableForm

Map[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}], {1}] // TableForm
Map[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}], {2, 1}] // TableForm
Map[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}], {2, 2}] // TableForm
Map[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}], {2, 3}] // TableForm


Even this

MatrixFunction[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}]]


The desired output is

Table[f[i, j], {i, 0, 3}, {j, 0, 3}] // TableForm


{{f[0, 0], f[0, 1], f[0, 2], f[0, 3]}, {f[1, 0], f[1, 1], f[1, 2], f[1, 3]}, {f[2, 0], f[2, 1], f[2, 2], f[2, 3]}, {f[3, 0], f[3, 1], f[3, 2], f[3, 3]}}

Final aim is to apply a pure function which takes two inputs over an array, like this

In163 is one of many trials without a success. I have tested

Total[Select[myCAans, (#[[5]] == 0 && #[[10]] == 0) &][[;; , 12]]]
Total[Select[myCAans, (#[[5]] == 0 && #[[10]] == 1) &][[;; , 12]]]
Total[Select[myCAans, (#[[5]] == 0 && #[[10]] == 2) &][[;; , 12]]]
Total[Select[myCAans, (#[[5]] == 0 && #[[10]] == 3) &][[;; , 12]]]

Total[Select[myCAans, (#[[5]] == 3 && #[[10]] == 3) &][[;; , 12]]]


which all works perfectly fine.

Any more examples where I can pick up mapping pure functions, using /@ @@ and &/@ much easier and faster?

Thanks.

• As I already said in the other post, you can use Apply on deeper levels. In this case, you want to have level {2}. Try Apply[f, Table[{i, j}, {i, 0, 3}, {j, 0, 3}], {2}]. – Henrik Schumacher Jun 14 '18 at 22:36
• @HenrikSchumacher Possible to write it using /@ or @@ or something similar? Do I have to use the words Apply I mean? – Chen Stats Yu Jun 14 '18 at 22:39
• Well, you could use f @@@ # & /@ array (where array = Table[{i, j}, {i, 0, 3}, {j, 0, 3}], but I would discourage that becaus it is less efficient. And for arrays of rank 3 or higher, you would finally hit the wall with such a syntax. – Henrik Schumacher Jun 14 '18 at 22:44
• @HenrikSchumacher Didn't seem to be working for my real function Total[Select[myCAans, (#[[5]] == #1) && (#[[10]] == #2) &][[;; , 12]]]. I will look for further details. – Chen Stats Yu Jun 14 '18 at 22:55
• Could you show please what myCAans is? (A small sample would suffice.) – Henrik Schumacher Jun 14 '18 at 22:57