# Using MapThread and ReplaceAll together

I am trying to carry out some operations on the elements of dataset2, which is an 11 x 14 x 6 dimensional array:

I have a function, CDexpr2[St,LD,hL] defined in Mathematica using something of the form

CDexpr2[St_,LD_,hL_]:=...


and a set of rules defined by

rules = {num -> 1, num -> 2, num -> 3, num -> 4, num -> 5, num -> 6, num -> 7, num -> 8, num -> 9, num -> 10, num -> 11};


and essentially I would like to evaluate CDexpr2 on the information inside dataset2 by applying it to the appropriate columns. The details are a bit tedious, so I will just copy my code here, which should work with any random dataset2 of the right size and dimensions.

list = ConstantArray[
HoldForm[With[{St = dataset2[[1]][[;; , 2]]},
CDexpr2[St, LD, hL]] /. LD -> dataset2[[num]][[;; , 3]] /.
hL -> dataset2[[num]][[;; , 4]]], 11]


Since I have used HoldForm, the result is an expression of the right length, which looks great:

{With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]],
With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]],
With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]],
With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]],
With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]],
With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]],
With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]],
With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]],
With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]],
With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]],
With[{St=dataset2[[1]][[1;;All,2]]},CDexpr2[St,LD,hL]]/. LD->dataset2[[num]][[1;;All,3]]/. hL->dataset2[[num]][[1;;All,4]]}


Now, all that I should need to do is replace all the nums in the above with rules -- I want to replace the 1st instance of num with 1, the 2nd instance of num with 2, and so on. I want the result to be a list of length {11,14}.

But when I apply rules like so:

MapThread[ReplaceAll, {list, rules}] // ReleaseHold


the result is a list of size {11,14,14,14} which means that it's not applying rules simultaneously, but instead it's applying them separately each time num shows up; but I would like for the nums to all be replaced by 1, then all be replaced by 2, and so on until the ones in the 11th element of list are replaced by 11.

I appreciate any help, and I'd be happy to provide some more context and details to help anyone reproduce this work!

• newlist = Table[list /. rules[[n]], {n, 1, Length[rules], 1}] // ReleaseHold may be doing what is required (??), but the resulting list is of dimensions {11,11}. Sep 22, 2020 at 20:05
• You have a data vector of 11 elements and you have 11 replacement rules. Where does the 14 come from? A bit of advice: try first a very small data set, e.g.2 elements, it is much easier to see what is going on. Sep 23, 2020 at 10:29
• @DanielHuber, 14 is one of the lengths of the data set. For example, dataset2[[1]][[1;;All,2]] has length 14. I found out a solution which I'll post as an answer for future reference Sep 24, 2020 at 3:00

I found a solution to this --- basically, using CDexpr2[St, LD, hL] and then replacing LD and hL with lists is a bad idea. It's much better to use MapThread to map the function CDexpr2 over the relevant lists. The final form I ended up using is something like this:

list=ConstantArray[

And on this list, using MapThread a second time works:
MapThread[ReplaceAll,{list,rules}]//ReleaseHold

and has the right dimensions, {11,14}