# Conditional Check on Matrix

Suppose I have the following matrix (i,j):

mat = {{19.8134, 54.7015, 64.1866, 75.5597, 88.8358, 83.7313, 93.0299,
88.2537, 101.03, 108.873, 91.8731, 88.097, 82.0075, 88.7164,
83.5672, 69.9254, 56.0149, 31.4328, 19.4179, 6.40299, 1.48507,
0.410448, 0.171642, 0, 0}, {24.2741, 52.6815, 64.4667, 74.8593,
87.6222, 87.1926, 96.3407, 90.0667, 98.2519, 103.496, 89.7926,
85.1259, 83.4889, 88.7111, 86.1852, 75.4296, 53.7778, 34.0667,
19.6889, 7.40741, 2.16296, 0.348148, 0.00740741, 0, 0}, {18.4632,
50.6544, 60.5735, 72.5441, 86.0956, 84.9485, 91.1103, 84.8603,
94.3897, 100.463, 83.6324, 82.9412, 77.4853, 79.6324, 77.9485,
61.9118, 48.7574, 27.8309, 13.8824, 5.96324, 1.60294, 0.529412,
0.0294118, 0, 0}, {19.1212, 59.0985, 71.6818, 86.8182, 96.553,
91.5455, 100.402, 92.6515, 111.068, 111.909, 95.7803, 94.3864,
92.75, 90.8182, 95.3561, 86.197, 73.6136, 51.5682, 40.6742, 15.1515,
3.30303, 0.848485, 0.0757576, 0.030303, 0.030303}, {19.6083,
55.5917, 66.5583, 75.575, 87.6, 89.1417, 96.1083, 92.6917, 108.375,
117.3, 96.7333, 91.8167, 86.6583, 91.7333, 93.45, 79.5333, 60.275,
36.95, 24.3583, 9.60833, 1.76667, 0.416667, 0.00833333, 0.0333333,
0}}


The following is my criteria vector that each element corresponding to each row of the above matrix:

check = {7.94021, 7.95033, 7.44576, 8.83645, 8.38877}


Here is my criteria. If the element in mat[[i,All]] is less than the corresponding element in check[[i]] vector, I want to replace with the following vector

replacevec = {79.4021, 79.5033, 74.4576, 88.3645, 83.8877}


where each element (replacevec[[i]]) correspond to each row in mat .

Could you give me suggestions?

Thank you for help.

-
could you please clarify "If the element in mat[[i,All]] is less than the corresponding element in check[[i]] vector" as mat[[i,All]] is a vector of length 25. What is being compared? Is it a single comparison for each row or is each element in row being compared with check element for that row? – ubpdqn Feb 17 '14 at 11:10
Do you mean mat[[i,All]] or mat[[All,i]]? mat[[i,All]] has different length compared with check or replacevec. – Yi Wang Feb 17 '14 at 11:15
@ubpdqn: each element of mat in row being compared with check element. For example, any numbers in first row of mat that is less than the first value of check vector (check[[1]]) will be replaced with the first element of replacevec (replacevec[[1]]) and so on... – newbie Feb 17 '14 at 11:22

Using the variable in the post:

MapThread[
Function[{x, y, z}, x /. a_?(# < y &) :> z], {mat, check,
replacevec}]


yields:

{{19.8134, 54.7015, 64.1866, 75.5597, 88.8358, 83.7313, 93.0299,
88.2537, 101.03, 108.873, 91.8731, 88.097, 82.0075, 88.7164,
83.5672, 69.9254, 56.0149, 31.4328, 19.4179, 79.4021, 79.4021,
79.4021, 79.4021, 79.4021, 79.4021}, {24.2741, 52.6815, 64.4667,
74.8593, 87.6222, 87.1926, 96.3407, 90.0667, 98.2519, 103.496,
89.7926, 85.1259, 83.4889, 88.7111, 86.1852, 75.4296, 53.7778,
34.0667, 19.6889, 79.5033, 79.5033, 79.5033, 79.5033, 79.5033,
79.5033}, {18.4632, 50.6544, 60.5735, 72.5441, 86.0956, 84.9485,
91.1103, 84.8603, 94.3897, 100.463, 83.6324, 82.9412, 77.4853,
79.6324, 77.9485, 61.9118, 48.7574, 27.8309, 13.8824, 74.4576,
74.4576, 74.4576, 74.4576, 74.4576, 74.4576}, {19.1212, 59.0985,
71.6818, 86.8182, 96.553, 91.5455, 100.402, 92.6515, 111.068,
111.909, 95.7803, 94.3864, 92.75, 90.8182, 95.3561, 86.197, 73.6136,
51.5682, 40.6742, 15.1515, 88.3645, 88.3645, 88.3645, 88.3645,
88.3645}, {19.6083, 55.5917, 66.5583, 75.575, 87.6, 89.1417,
96.1083, 92.6917, 108.375, 117.3, 96.7333, 91.8167, 86.6583,
91.7333, 93.45, 79.5333, 60.275, 36.95, 24.3583, 9.60833, 83.8877,
83.8877, 83.8877, 83.8877, 83.8877}}

-
Awesome!! It works. I'm curious since I am new to mathematica, are there any sources or references that help improving my coding skills ? – newbie Feb 18 '14 at 2:23
@newbie I am sorry for delay in replying. The Wolfram documentation is excellent. This includes that within Mathematica and from the Wolfram portal which includes video tutorials. Searching and participating in this site is extremely helpful. I am sorry I am using mobile device and hyperlinking is difficult. – ubpdqn Feb 23 '14 at 14:10

If I understood the question correctly, you can use

Transpose[Replace[#, v_ /; And @@ Positive[check - v] :> replacevec] & /@ Transpose@mat]


{{19.8134, 54.7015, 64.1866, 75.5597, 88.8358, 83.7313, 93.0299, 88.2537, 101.03, 108.873, 91.8731, 88.097, 82.0075, 88.7164, 83.5672, 69.9254, 56.0149, 31.4328, 19.4179, 6.40299, 79.4021, 79.4021, 79.4021, 79.4021, 79.4021}, {24.2741, 52.6815, 64.4667, 74.8593, 87.6222, 87.1926, 96.3407, 90.0667, 98.2519, 103.496, 89.7926, 85.1259, 83.4889, 88.7111, 86.1852, 75.4296, 53.7778, 34.0667, 19.6889, 7.40741, 79.5033, 79.5033, 79.5033, 79.5033, 79.5033}, {18.4632, 50.6544, 60.5735, 72.5441, 86.0956, 84.9485, 91.1103, 84.8603, 94.3897, 100.463, 83.6324, 82.9412, 77.4853, 79.6324, 77.9485, 61.9118, 48.7574, 27.8309, 13.8824, 5.96324, 74.4576, 74.4576, 74.4576, 74.4576, 74.4576}, {19.1212, 59.0985, 71.6818, 86.8182, 96.553, 91.5455, 100.402, 92.6515, 111.068, 111.909, 95.7803, 94.3864, 92.75, 90.8182, 95.3561, 86.197, 73.6136, 51.5682, 40.6742, 15.1515, 88.3645, 88.3645, 88.3645, 88.3645, 88.3645}, {19.6083, 55.5917, 66.5583, 75.575, 87.6, 89.1417, 96.1083, 92.6917, 108.375, 117.3, 96.7333, 91.8167, 86.6583, 91.7333, 93.45, 79.5333, 60.275, 36.95, 24.3583, 9.60833, 83.8877, 83.8877, 83.8877, 83.8877, 83.8877}}

-
I think the number on the first row, which is 6.40299 (output[[1,-6]]) should be replaced with 79.4021. – newbie Feb 17 '14 at 11:31
So I actually misunderstood your question... I think @ubpdqn's answer is what you want. – Yi Wang Feb 17 '14 at 11:54

Something probably slower than ubpdqn answer but more understandable to me:

Table[
ReplacePart[mat[[i]],
Rule[#, replacevec[[i]]] & /@ Flatten[Position[mat[[i]], #] & /@
Select[mat[[i]], # < check[[i]] &]]]
, {i, 1, Length@mat}]


where I simply search for the position of the elements less than check[[i]] e.g.:

Flatten[Position[mat[[1]], #] & /@ Select[mat[[1]], # < check[[1]] &]]


and then I replace them by replacevec[[i]] using ReplacePart.

-