I have the following code:
l1 = {{1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3, 4, 5}};
TableForm@Transpose[PadRight[#, 6] & /@ l1]
assoc = {{{1, 1}, {2, 2}, {3, 0}, {4, 3}, {5, 4}, {6, 0}}, {{1,
1}, {2, 0}, {3, 0}, {4, 2}, {5, 3}, {6, 0}}, {{1, 1}, {2, 0}, {3,
2}, {4, 3}, {5, 4}, {6, 5}}};
l2 = ConstantArray[0, {3, 6}];
MapThread[
Function[{u, v, w}, If[#2 > 0, v[[#1]] = u[[#2]]] & @@@ w], {l1, l2,
assoc}];
TableForm@Transpose@%
The goal is to rearrange the l1
so according to the assoc
which works as follows e.g. for l1[[1]]
:
l1[[1,1;;2]]
will map to l2[[1,1;;2]]
, l1[[1,3;;4]]
will map to l2[[1,4;;5]]
and the rest of l2[[1]]
will have zeros. So assoc[[All,All,1]]
contains the positions is l2
and assoc[[All,All,2]]
contains positions in l1
but if assoc[[All,All,2]]==0
then the appropriate position in l2
is left as zero.
The code above works to some point, it throws errors and instead of zeros, the result has Null
values. What would be a nice and elegant way to do this (i am not necessarily requiring to keep the assoc
array in the exact form, i am looking for a nice and easy way to rearrange an array according the rules explained above).
The goal is to transform the table that looks like (the values in l1
with padded zeros):
Where the rules of what from l1
should go where in l2
are give in some fashion by saying some position in l1
should go to some other position in l2
(such as e.g. in assoc
)
l1[[1, 1;;2]] will map to l2[[1, 1;;2]]
? Are you referring to a general functional correspondence, or do you mean that those specific values ofl1
should be replaced with the corresponding values ofl2
? It appears that what you want isMapAt
. If you clarify the question and give a small example of input and output, you might find more help forthcoming. $\endgroup$