0
$\begingroup$

This is a snippet of a code that I have:

ClearAll["Global`*"]
ReArrangedTable[{x_, YReal1_, YImag1_, YReal2_, YImag2_}] := {x,
  YReal1, YImag1, FunctionValue[YReal1, x],
  YReal2, YImag2, FunctionValue[YReal2, x]}
MappedTable = Map[ReArrangedTable, SolutionInTableForm]

Here, SolutionInTableForm consist of numerical values with dimensions which depends on the input- lets say that for this case its {1000,5}. This 5 values is taken into x_,YReal1_,YImag1_,YReal2_,YImag2_. And, FunctionValue is a sample function that I define earlier and then I use Map. This seems to be fair enough.

But, when value of 5 changes to 7 or 9 or 13, then I am forced to type arguments of ReArrangedTable manually and also RHS of ReArrangedTable. Is there any way to "automate" this?

Edit 1:

Ideally I am expecting to have a code like this:

ValueOfY = 20
ReArrangedTable[{x_, YReal1_, YImag1_, YReal2_, YImag2_, ...., 
   YReal19_, YImag19_, YReal20_, YImag20_}] := {x,
  YReal1, YImag1, FunctionValue[YReal1, x],
  YReal2, YImag2, FunctionValue[YReal2, x], ... .. YReal19, YImag19, 
  FunctionValue[YReal19, x],
  YReal20, YImag20, FunctionValue[YReal20, x]}
MappedTable = Map[ReArrangedTable, SolutionInTableForm]

Is this possible to do this automatically?

I am using Mathematica 11.0.0.

Thanks in advance

$\endgroup$
2
  • $\begingroup$ How do you have the input with a length of 12? From the structure you give, would then length always be odd? $\endgroup$
    – JimB
    Jul 4, 2021 at 1:20
  • $\begingroup$ Sorry, you are correct and that was mistake. I have updated that. $\endgroup$
    – sreeraj t
    Jul 4, 2021 at 2:22

1 Answer 1

1
$\begingroup$

Does the following do what you want?

ClearAll["Global`*"]
ReArrangedTable[xy_] := 
  Join[{xy[[1]]}, Flatten[Table[{xy[[i]], xy[[i + 1]], 
  FunctionValue[xy[[i]], xy[[1]]]}, {i, 2, Length[xy], 2}]]]

SolutionInTableForm = {{x, 1, 2, 1, 3, 2, 7},
  {x, 2, 3, 5, 1, 3, 9},
  {x, 4, 5, 2, 9, 1, 1}};

MappedTable = Map[ReArrangedTable, SolutionInTableForm]
(* {{x, 1, 2, FunctionValue[1, x], 1, 3, FunctionValue[1, x], 2, 7, FunctionValue[2, x]}, 
    {x, 2, 3, FunctionValue[2, x], 5, 1, FunctionValue[5, x], 3, 9, FunctionValue[3, x]}, 
    {x, 4, 5, FunctionValue[4, x], 2, 9, FunctionValue[2, x], 1, 1, FunctionValue[1, x]}} *)
$\endgroup$
2
  • $\begingroup$ While executing your code with ReArrangedTable[{x, YReal1, YImag2, 1, 3}], I am getting o/p {x, YReal1, YImag2, FunctionValue[YReal1, x], 1, 3, FunctionValue[1, x]}. This give only LHS and that too I am supposed to write YReal and YImag in ReArrangedTable[{x, 1, 2, 1, 3, 2, 7}], which is tedious. Further how about RHS? $\endgroup$
    – sreeraj t
    Jul 4, 2021 at 2:19
  • $\begingroup$ I'm not understanding what you mean about "RHS". I just added in the part with the Map function. $\endgroup$
    – JimB
    Jul 4, 2021 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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