1
$\begingroup$

I have a nested list and I want to subtract from that list based on elements of another list, where each element of each nest subtracts the other list.

For example, getting from A and B to C

A = {{5,3,5,7,2},{2,6,4,8,3}}

B = {1,2,1,2,1}

C = {{4,1,4,5,1},{1,4,3,6,2}}.

I've used multiple tables to perform this. However, the code takes too long, especially for many nests (columns)*. Is there a faster way?

*In my case, I have many nests as it is time series data. Each nest representing a new column.

$\endgroup$
  • $\begingroup$ Yes! Thank you. $\endgroup$ – reemodels Sep 20 at 8:41
2
$\begingroup$
c = Map[Subtract[#, B]&] @ A (* or *)

c =  # - B & /@ A

{{4, 1, 4, 5, 1}, {1, 4, 3, 6, 2}}

Also

A - ConstantArray[B, Length @ A]

{{4, 1, 4, 5, 1}, {1, 4, 3, 6, 2}}

Note: C is a protected system symbol.

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