1
$\begingroup$

I have a code, which goes like this:

ClearAll["Global`*"]

c = Table[{x, 2 x, 0 x, N[Sqrt[x]], 0 x}, {x, 0, 20, 2}];

headings = Join[{{"x", "2*x", "imag1", "sqrt(x)", "imag2"}}, c];

Export["file.dat", headings];

const1 = 2;
const2 = 3;

The output is extracted to a dat file “file.dat” which looks like this:

enter image description here

Now I need to take each element in the second column, subtract the value of first column from this one and then multiply by a constant and then whole of this value divide by another constant defined in the program. All this value should be collected in another column, say D.

E.g. $D2=\frac{(x2-B2)\times const1}{const2}$, $D3=\frac{(x3-B3)\times const1}{const2}$, and so on. Similarly, I need to do it for forth column. Ideally, I am expecting the output to look like this:

enter image description here

How can I do this?

Thanks in advance

$\endgroup$
  • $\begingroup$ const2 is undefined and const1 is defined twice. $\endgroup$ – Roman May 4 at 15:20
  • $\begingroup$ @Roman it was a typo and has been corrected. Thanks for pointing out. $\endgroup$ – sreeraj t May 4 at 15:24
2
$\begingroup$

See if you can use this

tbl = Table[{x, 2 x, 0 x, N[Sqrt[x]], 0 x}, {x, 0, 20, 2}];
const1 = 2.;
const2 = 3.;
newrow[{a_,b_,c_,d_,e_}]:={a,b,c,(b-a)*const1/const2,d,e,(c-b)*const1/const2};
newtbl=Map[newrow,tbl];
headings = {"x", "2*x", "imag1", "sqrt(x)", "imag2"};
Export["file.dat",Join[{headings},newtbl]]

That Map is going to construct a new row from an existing row and do that for every row in your existing table creating a new table.

$\endgroup$
  • $\begingroup$ This is it. Thanks a lot. $\endgroup$ – sreeraj t May 5 at 1:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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