# Correct way to compare arrays and do conditional evaluations

I would like to compare two arrays a=:

and b=

and get:

using:

(b/.(b_?Positive->a+b))//MatrixForm


but this doesn't seem to work? Can anyone tell me what I am doing wrong?

Basically the conditional only updates b to be sum of a and b only when a non zero is detected in b or else it should do nothing, but can't work out why it is not summing. Or must I first flatten it?

-
Why don't you use proper Mathematica syntax in your arrays and why don't you show the code you tried to solve the problem with? – Matariki Dec 29 '12 at 20:31
The code is (b/.(b_?Positive->a+b))//MatrixForm – sebastian c. Dec 29 '12 at 20:57

The better way to do this is with numeric operations, in this case Unitize.

a = {0., 32.94, 0., 0., 16.14, 0., 0.746, 0.746};
b = {0., 0., 0., 0., 0., 0., 0., 4.148};

a * Unitize[b] + b

{0., 0., 0., 0., 0., 0., 0., 4.894}

-
This seems much simpler solution and works with my data set, whereas not sure why solution from image_doctor didn't work for my sliced matrix data arrays? – sebastian c. Dec 29 '12 at 23:46
@sebastian probably because 0 is not the same as 0. or because you had x or y defined somewhere. Please see my comment below his answer for an alternative version. – Mr.Wizard Dec 29 '12 at 23:49

Lower case is preferred for single letter variables.

You can transpose your two lists to make pairs of elements and then use patterns to add the elements, or replace with the value of b, based on the conditions you supply:

a = {0, 0, 1, 0, 2, 3}
b = {0, 2, 0, 0, 0, 1}

Transpose[{a, b}] /. {{x_, y_?Positive} :> x + y, {_, y_} :> y}


{0, 2, 0, 0, 0, 4}

-
Hi @image_doctor, thanks for this but why do I need to transpose in the first place and not do this with two separate arrays? – sebastian c. Dec 29 '12 at 21:17
@sebastianc. Replacement rules take something matched by a pattern and replace it with something else. In your case you would like to consider the value of an element in b and replace it with some expression based on the values in equivalent positions in both a and b. So the pattern you are interested in is an element of b, not the entire value of b, transposing exposes those elements. Otherwise you wold trying to match pattern which depended on all the values in b. Hope that helps. – image_doctor Dec 29 '12 at 21:47
Hi @image_doctor, not sure why when applying with a set of data which is derived from matrix and sliced up, so myData is of Dimensions[myData]={6 x 1} it does not work, but I do notice you have a and b as Dimensions of {6}. What I mean is that with {6 x 1} data instead, now I get these two columns of data in brackets of Dimensions=Transpose[your equation] {6,2,1}? – sebastian c. Dec 29 '12 at 22:12
@sebastianc. From the formatting of your question, it isn't clear what the exact structure of your data is, so I assumed it was held in the most usual way as a one dimensional list, {1,0,1,...,1}. Most data is Mathematica is held as n-dimensional lists and not matrices as might be the case in Matlab. Your data may be in the form {{1},{0},{1},...,{1}} which would be a list of lists and give you {6,1} as your dimensions. Maybe there is some reason you have the data in that format? – image_doctor Dec 29 '12 at 22:42
image_doctor, you're not yourself today. Your code has some serious problems. 0 =!= 0. and you forgot to use :>. I propose this: Replace[{a, b}\[Transpose], {{x_, y_?Positive} :> x + y, _ -> 0}, 1]` – Mr.Wizard Dec 29 '12 at 23:46