0
$\begingroup$

I have a list where the sum of its values results 10:

timeEachStep = {{0.7646050049957724}, {1.1043813977215065}, {1.792518103577381}, {1.5769143982179603}, {2.475716623701331}, {2.2858644717860503}}  
Plus @@ timeEachStep  

$\left( \begin{array}{c} 0.764605 \\ 1.10438 \\ 1.79252 \\ 1.57691 \\ 2.47572 \\ 2.28586 \\ \end{array} \right)$

${10}$

And I have another value that will change the above list

tClaw = 0.2 

$0.2$

With my limited knowledge I had to create a list of work

list2 = Table[tClaw, Length[timeEachStep]]  

$\{0.2,0.2,0.2,0.2,0.2,0.2\}$

This list should be added to the other list, but without changing the sum of values. Then I did a subtraction, then I added the values intercalating.

list3 = timeEachStep - list2  
Plus @@ list3  

$\left( \begin{array}{c} 0.564605 \\ 0.904381 \\ 1.59252 \\ 1.37691 \\ 2.27572 \\ 2.08586 \\ \end{array} \right)$

${8.8}$

newList = Flatten[Transpose[{list3, list2}]]  
Plus @@ newList  

$\{0.564605,0.2,0.904381,0.2,1.59252,0.2,1.37691,0.2,2.27572,0.2,2.08586,0.2\}$

$10$

There is a specific function that makes this?

$\endgroup$
3
  • 2
    $\begingroup$ Flatten@Riffle[timeEachStep - tClaw, tClaw, {2, -1, 2}] should do it. $\endgroup$ Oct 7, 2016 at 13:47
  • $\begingroup$ Yes. It's that simple. $\endgroup$
    – JPeter
    Oct 7, 2016 at 14:02
  • $\begingroup$ Flatten@{# - tClaw, tClaw} & /@ timeEachStep $\endgroup$
    – BlacKow
    Oct 7, 2016 at 16:23

1 Answer 1

2
$\begingroup$

As has already been mentioned by Marius Ladegård Meyer

Flatten@Riffle[timeEachStep - tClaw, tClaw, {2, -1, 2}]

Yields:

$\{0.564605,0.2,0.904381,0.2,1.59252,0.2,1.37691,0.2,2.27572,0.2,2.08586,0.2\}$

$\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.