Discussion:
Replace elements in a matrix
(too old to reply)
kiz_0987
2011-02-15 11:32:43 UTC
Permalink
I'm new to Mathematica and have a question regarding matrices. I was
sure that this problem must have been answered in the past, but could
not find a solution.

I want to replace some elements in a matrix (based on value) with
elements from another matrix at the same position. Eg:
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};

I want to replace the elements in "a" which are equal to 1 (say) with
the same position elements in b to give:
c = {{0, 3}, {2, 2}};

How can I do this? I started with using Position and ReplacePart but
could not figure out how to get it to work in this case (other cases
are documented, such as replacing with a single value).

Any help appreciated. Thanks.
Adriano Pascoletti
2011-02-16 09:32:21 UTC
Permalink
A solution based on Transpose and ReplaceAll

In[1]:= a = {{0, 1}, {1, 2}}; b = {{1, 3}, {2, 5}};


Transpose[{a, b}, {3, 1, 2}]


% /. {{1, x_Integer} :> x, {x_Integer, _Integer} :> x}


Out[3]= {{{0, 1}, {1, 3}}, {{1, 2}, {2, 5}}}
Out[4]= {{0, 3}, {2, 2}}


Adriano Pascoletti
Post by kiz_0987
I'm new to Mathematica and have a question regarding matrices. I was
sure that this problem must have been answered in the past, but could
not find a solution.
I want to replace some elements in a matrix (based on value) with
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};
I want to replace the elements in "a" which are equal to 1 (say) with
c = {{0, 3}, {2, 2}};
How can I do this? I started with using Position and ReplacePart but
could not figure out how to get it to work in this case (other cases
are documented, such as replacing with a single value).
Any help appreciated. Thanks.
Daniel Lichtblau
2011-02-16 09:32:42 UTC
Permalink
Post by kiz_0987
I'm new to Mathematica and have a question regarding matrices. I was
sure that this problem must have been answered in the past, but could
not find a solution.
I want to replace some elements in a matrix (based on value) with
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};
I want to replace the elements in "a" which are equal to 1 (say) with
c = {{0, 3}, {2, 2}};
How can I do this? I started with using Position and ReplacePart but
could not figure out how to get it to work in this case (other cases
are documented, such as replacing with a single value).
Any help appreciated. Thanks.
Among other methods:

val = 1;
stencil = Unitize[a-val];

stencil*a + (1-stencil)*b

Out[10]= {{0, 3}, {2, 2}}

Daniel Lichtblau
Wolfram Research
DrMajorBob
2011-02-16 09:33:25 UTC
Permalink
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};
c = {{0, 3}, {2, 2}};

c == With[{u = Unitize[a - 1]},
a u + b (1 - u)]

True

or

d = a;
p = Position[a, 1]
c == ReplacePart[a, Thread[p -> Extract[b, p]]]

True

Bobby
Post by kiz_0987
I'm new to Mathematica and have a question regarding matrices. I was
sure that this problem must have been answered in the past, but could
not find a solution.
I want to replace some elements in a matrix (based on value) with
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};
I want to replace the elements in "a" which are equal to 1 (say) with
c = {{0, 3}, {2, 2}};
How can I do this? I started with using Position and ReplacePart but
could not figure out how to get it to work in this case (other cases
are documented, such as replacing with a single value).
Any help appreciated. Thanks.
--
***@yahoo.com
Peter Breitfeld
2011-02-16 09:33:57 UTC
Permalink
Post by kiz_0987
I'm new to Mathematica and have a question regarding matrices. I was
sure that this problem must have been answered in the past, but could
not find a solution.
I want to replace some elements in a matrix (based on value) with
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};
I want to replace the elements in "a" which are equal to 1 (say) with
c = {{0, 3}, {2, 2}};
How can I do this? I started with using Position and ReplacePart but
could not figure out how to get it to work in this case (other cases
are documented, such as replacing with a single value).
Any help appreciated. Thanks.
Try this:

apos=Position[a,1];
bvals=Extract[b,apos];
c=ReplacePart[a,Thread[apos->bvals]]
--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de
Armand Tamzarian
2011-02-16 09:35:42 UTC
Permalink
Post by kiz_0987
I'm new to Mathematica and have a question regarding matrices. I was
sure that this problem must have been answered in the past, but could
not find a solution.
I want to replace some elements in a matrix (based on value) with
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};
I want to replace the elements in "a" which are equal to 1 (say) with
c = {{0, 3}, {2, 2}};
How can I do this? I started with using Position and ReplacePart but
could not figure out how to get it to work in this case (other cases
are documented, such as replacing with a single value).
Any help appreciated. Thanks.
There may be a more elegant way but this works:

transform[a_, b_, value_] := Module[{pos, c = a},

pos = Position[c, value];
(c[[Sequence @@ #]] = b[[Sequence @@ #]]) & /@ pos;

c]

In[]= transform[a, b, 1]

Out[]= {{0, 3}, {2, 2}}

Mike
Bill Rowe
2011-02-16 09:36:25 UTC
Permalink
Post by kiz_0987
I'm new to Mathematica and have a question regarding matrices. I was
sure that this problem must have been answered in the past, but
could not find a solution.
I want to replace some elements in a matrix (based on value) with
elements from another matrix at the same position. Eg: a = {{0,
1}, {1, 2}};
b = {{1, 3}, {2, 5}};
I want to replace the elements in "a" which are equal to 1 (say)
with the same position elements in b to give: c = {{0, 3}, {2, 2}};
How can I do this?
Here is one way

In[19]:= b + Unitize[a - 1] (a - b)

Out[19]= {{0, 3}, {2, 2}}

Unitize makes all non-zero entries 1. So, in each position where
a is 1, the expression above becomes b + 0 = b and for each
position where a is not 1 it becomes

b + (a -b) = a
Bob Hanlon
2011-02-16 09:37:18 UTC
Permalink
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};

ReplacePart[a, Thread[Position[a, 1] -> Flatten[Pick[b, a, 1]]]]

{{0, 3}, {2, 2}}


Bob Hanlon

---- kiz_0987 <***@gmail.com> wrote:

=============
I'm new to Mathematica and have a question regarding matrices. I was
sure that this problem must have been answered in the past, but could
not find a solution.

I want to replace some elements in a matrix (based on value) with
elements from another matrix at the same position. Eg:
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};

I want to replace the elements in "a" which are equal to 1 (say) with
the same position elements in b to give:
c = {{0, 3}, {2, 2}};

How can I do this? I started with using Position and ReplacePart but
could not figure out how to get it to work in this case (other cases
are documented, such as replacing with a single value).

Any help appreciated. Thanks.
Mala Jozsef
2011-02-16 09:37:50 UTC
Permalink
I&#39;m new to Mathematica and have a question regarding matrices. I was
sure that this problem must have been answered in the past, but could
not find a solution.
I want to replace some elements in a matrix (based on value) with
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};
I want to replace the elements in "a" which are equal to 1 (say) with
c = {{0, 3}, {2, 2}};
How can I do this? I started with using Position and ReplacePart but
could not figure out how to get it to work in this case (other cases
are documented, such as replacing with a single value).
Any help appreciated. Thanks.
Hi, the simple way of doing it may be this: c1=Table[If[a[[i,j]]==1,b[[i,j]],a[[i,j]]],{i,m},{j,n}] or c2=Sign[a-1]^2*(a-b)+b hth, Jozsef
Ray Koopman
2011-02-16 11:45:20 UTC
Permalink
Post by kiz_0987
I'm new to Mathematica and have a question regarding matrices. I was
sure that this problem must have been answered in the past, but could
not find a solution.
I want to replace some elements in a matrix (based on value) with
a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};
I want to replace the elements in "a" which are equal to 1 (say) with
c = {{0, 3}, {2, 2}};
How can I do this? I started with using Position and ReplacePart but
could not figure out how to get it to work in this case (other cases
are documented, such as replacing with a single value).
Any help appreciated. Thanks.
I'm surprised no one suggested this:

a = {{0, 1}, {1, 2}};
b = {{1, 3}, {2, 5}};
c = MapThread[If[#1 == 1, #2, #1] &, {a, b}, 2]

{{0, 3}, {2, 2}}

Loading...