I have two lists (value
and label
). With the function Permute
, I can permute the positions of the elements of value
according to the permutation label
.
Namely, the letters "a", "b", "c", "d", "e", "f"
correspond to position numbers 1, 2, 3, 4, 5, 6
, and I want to arrange the number of value
in the position described by label
.
As a simple example:
value= {0, 1, 2, 0, 0, 2}; label = {"a", "b", "d", "f", "c", "e"}; Permute[value, LetterNumber[label]]
give the result:
{0, 1, 0, 2, 2, 0}
When the two listed are both nested lists, I wonder whether one can directly use Permute
as following:
value= {{0, 1, 2, 0, 0, 1}, {0, 1, 2, 0, 0, 2}, {0, 1, 2, 0, 1, 0}, {0, 1, 2, 0, 1, 1}};
label = {{"a", "b", "c", "d", "e", "f"}, {"a", "b", "d", "e", "c", "f"}, {"a", "b", "d", "f", "c", "e"}};
ii=2;
Permute[value, LetterNumber[label[[ii]]]]
It gives some errors and doesn't work. However, when I make ii=1
, there is no error. It seems one cannot use Permute
directly in nested lists.
One way is to use for-loop. However the calaulation will slow down when the two lists get larger and larger.
AllVC= {};
For[ii = 1, ii <= Length[label], ii++,
For[jj = 1, jj <= Length[value], jj++,
CurrVC = Permute[value[[jj]], LetterNumber[label[[ii]]]];
AppendTo[AllVC, CurrVC];
];
];
Is it possiable to use Permute
for nested lists without using loops? Is there any simple and quike way to do the above task when lists get large? Thank you in advance!
Map[Permute[#, LetterNumber[label[[ii]]]] &]@value
$\endgroup$