Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to compare two arrays a=:

enter image description here

and b=

enter image description here

and get:

enter image description here



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?

share|improve this question
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}
share|improve this answer
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}

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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