Skip to main content
2 of 4
deleted 5 characters in body; edited title
C. E.
  • 71.2k
  • 6
  • 141
  • 269

Replacing elements in lists of a nested list

I have the following nested list

CombiningCyclesCase2={{1, 2, 3, 0, 0}, {1, 2, 4, 3, 0}, {1, 2, 4, 5, 3}, {3, 2, 1, 4, 0}, {3, 2, 1, 5, 4}, {3, 2, 4, 1, 5}, {3, 4, 1, 2, 5}, {3, 2, 4, 5, 1}, {3, 5, 4, 1, 2}}

I want to replace the zeroes in the sublists that have zeroes, by the corresponding position of the zeroes. For instance, in the sublist {1,2,3,0,0} I want to replace the 0's to have {1,2,3,4,5}, but in the sublists that don't have any zeros, I want to leave them like they are. I tried doing this with the following line of code, where n=5:

Table[If[Max[CombiningCyclesCase2[[i]]] == n, CombiningCyclesCase2[[i]]], ReplacePart[CombiningCyclesCase2[[i]], Thread[Range[Max[CombiningCyclesCase2[[i]]] + 1, n]] -> Range[Max[CombiningCyclesCase2[[i]]] + 1, n]], {i, Length[CombiningCyclesCase2]}]

However, I get the error Part::pkspec1: The expression i cannot be used as a part specification. >>. Does anyone have a suggestion on how to solve this problem or if there is an easier way to do this?