2
$\begingroup$

I have a function

MinVarGeneralThermal1[T, 5, d]

which yields me the following list:

{{0.00917615, 0.0136892, 0.0204219, 0.0304659, 0.0454498, 0.10115,
 0.225114, 0.335831, 0.150899, 0.0678031}}

As you can see these are not single pairs of entires of the form

{{1},{2},{3},{4},...}

which makes it not possible for me to target single entries using a command like

MinVarGeneralThermal1[T, 5, d][[3]]

Is it possible to write a swap command that works something like this

swap[MinVarGeneralThermal1[T, 5, d],{1,3}]

which would essentially swap the first and third entries of the list (for example) ? In the end I want to be able to swap any two entries I want.

Thanks in advance !

$\endgroup$
4
$\begingroup$

I am not sure that I fully get your problem. You can always access individual elements of the nested list you have, e.g. by using yourList[[1, 3]].

However, this may work for you:

ClearAll[swap]
swap[list_List, pair_?(VectorQ[#, IntegerQ] &)] := {Permute[list[[1]], Cycles[{pair}]]}

Here is an example usage on a made-up list:

list = {CharacterRange["a", "g"]}
swap[list, {2, 3}]

(* Out:
{{"a", "b", "c", "d", "e", "f", "g"}}
{{"a", "c", "b", "d", "e", "f", "g"}}
*)
$\endgroup$
2
$\begingroup$
sWap[a : {{__}}, pair_] :=  Module[{l = a}, l[[1, pair]] = l[[1, Reverse @ pair]]; l]

MinVarGeneralThermal1[T, 5, d]

{{0.00917615,0.0136892,0.0204219,0.0304659,0.0454498,0.10115,0.225114,0.335831,0.150899,0.0678031}}

sWap[MinVarGeneralThermal1[T, 5, d], {1, 3}]

{0.0204219,0.0136892,0.00917615,0.0304659,0.0454498,0.10115,0.225114,0.335831,0.150899,0.0678031}}

$\endgroup$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.