mercredi 8 novembre 2017

How to apply different equations to elements in a list based on if statements in Mathematica

I have a number of lists with each element representing a company.

For example:

b = {1, 2, 3, 4, 5} 
c = {3, 4, 6, 8, 9}

means Company 1 has b=1, c=3 and Company 2 has b=2, c=4 etc

I am trying to code a series of conditional statements without using loops.

For example, if Company 'x' has z > 0, then I want it to update a,d,g in one way, and if z < 0 i want it to update a,d,g in another way.

I tried using the If command but it doesn't seem to work for lists with multiple elements.

If [z > 0,
 a = b + c;
 d = e - f;
 g = h - i - j;
 ,
 a = n - o;
 d = p - q;
 g = w + y]

I have experimented with the MapThread command but can only seem to get it to work for one line of code. E.g. for a:

a = MapThread[If[#1 > 0, #2 + #3 , #4 - #5] &, {z, b, c, n, o}]

but this becomes cumbersome if i have to write this for each separate line.

Is there a simple way to implement this without using loops?

Aucun commentaire:

Enregistrer un commentaire