# Create a new list under conditions from an existing one

I have an external file (data.out) which I read it with Mathematica using

data = ReadList["data.out", Number, RecordLists -> True];


This file contains three columns (x,y,f) of thousands of data. Here is a small sample of it:

0.30000000        0.00000000      0.1679035061279048E+02
2.30000000        0.50000000      0.1195447110986367E+01
0.10000000        0.00000000      0.2179035061279048E+02
5.80000000        2.50000000      0.7195447110986367E+01
3.00000000        0.00000000      0.4679035061279048E+02
7.90000000        7.50000000      0.1195447110986367E+01
4.70000000        2.00000000      0.8679035061279048E+01
1.20000000        4.50000000      0.2195447110986367E+01


Now I want to use Mathematica in order to manipulate the data list creating a new one dataMod which will have this structure (x,y,f,f1). Obviously, there is a new element (f1) in the dataMod list. Well, if $0\leq f < 5$, $f1$ should be a random number in the interval $10^{-4}< f1 \leq \sqrt2$, if $5\leq f < 7$, $f1$ should be a random number in the interval $10^{-7}\leq f1 \leq 10^{-4}$ and if $f \geq 7$ then $f1$ should be a random number in the interval $10^{-12} < f1 < 10^{-7}$.

Any ideas how to do this?

• I can only second @Anon´s comment: please show what you already tried and format any data so as to be useful for further use. – Yves Klett Aug 24 '13 at 14:22
• @YvesKlett Thanks for the reminder. I 'll take it into consideration in future questions. – Vaggelis_Z Aug 24 '13 at 14:38

## 1 Answer

It's simple, for example:

f1[x_] := Which[
0 <= x < 5, RandomReal[{10^(-4), Sqrt}],
5 <= x < 7, RandomReal[{ 10^(-7), 10^(-4)}],
x >= 7, RandomReal[{10^(-12), 10^(-7)}]
]
dataMod = {##, f1[#3]} & @@@ data (* Kuba's version *)


Or in the final step

dataMod = {Sequence @@ #, f1[#[]]} & /@ data (* Old version *)


It's a good idea to include what you've tried to do, so it shows you've made some effort to solve the problem yourself. Also when you want to include data you should include it as a list, in this case it's irrelevant that it was a file of that format to begin with.

• hm, 52 seconds :) – Pinguin Dirk Aug 24 '13 at 14:18
• @PinguinDirk Yup XD – C. E. Aug 24 '13 at 14:18
• deleted mine, same idea, next time it'll be mine :) – Pinguin Dirk Aug 24 '13 at 14:19
• @PinguinDirk Ah ok, that was gracious. I didn't expect that, I will remember it for the next time :) – C. E. Aug 24 '13 at 14:20
• Instead of Sequence you can {##, f1[#3]} & @@@ data :) – Kuba Aug 24 '13 at 15:19