1
$\begingroup$

Let's consider the following sample data

data = {{0.54106280      -416.66667000     0.1146249816891388E+01      0      8      6},
        {0.61835749      -372.22222000     0.6319693889170223E+00      0      2      1},
        {0.30917874       -16.66666700     0.2790560888497788E+00      0     18     15},
        {5.71980680       350.00000000     0.2327036789870881E+00      0     32     20},
        {6.10628020       350.00000000     0.5786771357367857E+00      0      0      0},
        {6.18357490       566.66667000     0.5117201288823356E+00      0     10      5}};

We are only interested in the last two entries of each row. What I want is to reduce the two numbers to the simplest output. For example, 8 6 should become 4 3, 18 15 should become 6 5, and so on. Obviously, the already simplified ones (e.g., 2 1, 0 0) should remain unchanged. So, we need a new data list containing the same four entries of data and the simplified outputs of the last two columns.

Any suggestions?

$\endgroup$
5
  • $\begingroup$ Does this do it? last2=Map[Take[#,-2]&,data]; last2reduced=Map[#/(GCD@@#)&,last2] There are lots of other ways of extracting those last two columns and even doing the GCD. Just pick any one way that you can practice until you have memorized it and can do it without making a mistake. $\endgroup$
    – Bill
    Commented Feb 21, 2022 at 18:34
  • $\begingroup$ @Bill not exactly. In case of 0 0 the code reports error "Infinite expression 1/0 encountered". $\endgroup$
    – Vaggelis_Z
    Commented Feb 21, 2022 at 18:38
  • $\begingroup$ Oops. Missed that one. Thank you for catching my mistake. See if this fixes it. last2reduced=Map[(g=GCD@@#;If[g==0,#,#/g])&,last2] $\endgroup$
    – Bill
    Commented Feb 21, 2022 at 18:42
  • $\begingroup$ @Bill Now it's working. Please post an answer so as to accept it. Also I want a new list data2 containing the first 4 elements and replacing the last two with the reduced ones.. $\endgroup$
    – Vaggelis_Z
    Commented Feb 21, 2022 at 18:51
  • $\begingroup$ The following changes data to the table you want: data[[All, {-2, -1}]] /= (GCD @@@ (data[[All, {-2, -1}]]) /. 0 -> 1); $\endgroup$ Commented Feb 21, 2022 at 19:44

2 Answers 2

3
$\begingroup$

Your data is not in a good shape for the syntax. If you input data as so in Mathematica, the space between them will turn into multiplication, every floating number will be assigned the machine precision, and the third column with the E will turn into Euler's $\mathrm{e}$. To prevent such and preserve them, the first thing you should do is making Mathematica interpret data as string data. Personally, I created data.csv accommodating the data. And then:

data = Flatten[StringSplit /@ Import["data.csv"], 1]

(*  {{"0.54106280", "-416.66667000", "0.1146249816891388E+01", "0", "8", "6"},
    {"0.61835749", "-372.22222000", "0.6319693889170223E+00", "0", "2", "1"},
    {"0.30917874", "-16.66666700", "0.2790560888497788E+00", "0", "18", "15"},
    {"5.71980680", "350.00000000", "0.2327036789870881E+00", "0", "32", "20"},
    {"6.10628020", "350.00000000", "0.5786771357367857E+00", "0", "0", "0"},
    {"6.18357490", "566.66667000", "0.5117201288823356E+00", "0", "10", "5"}}  *)

data2 = Module[{e56 = ToExpression[#[[5 ;; 6]]], gcd},
         gcd = GCD @@ e56;
         If[gcd != 0, ToString /@ Flatten@{#[[1 ;; 4]], e56/gcd}, #]
] & /@ data

(*  {{"0.54106280", "-416.66667000", "0.1146249816891388E+01", "0", "4", "3"},
     {"0.61835749", "-372.22222000", "0.6319693889170223E+00", "0", "2", "1"},
     {"0.30917874", "-16.66666700", "0.2790560888497788E+00", "0", "6", "5"},
     {"5.71980680", "350.00000000", "0.2327036789870881E+00", "0", "8", "5"},
     {"6.10628020", "350.00000000", "0.5786771357367857E+00", "0", "0", "0"},
     {"6.18357490", "566.66667000", "0.5117201288823356E+00", "0", "2", "1"}}  *)

To export, do Export["data2.csv", data2, "Table"]. In my notepad, it looks like the following:

enter image description here

$\endgroup$
4
$\begingroup$
alist = Transpose[data[[All, 1 ;; -3]]]
blist = Transpose[(If[# =!= {0, 0}, #/GCD @@ #, #]) & /@ 
   data[[All, -2 ;; -1]]]
Transpose[alist~Join~blist] // TableForm

$$\left( \begin{array}{cccccc} 0.541063 & -416.667 & 1.14625 & 0 & 4 & 3 \\ 0.618357 & -372.222 & 0.631969 & 0 & 2 & 1 \\ 0.309179 & -16.6667 & 0.279056 & 0 & 6 & 5 \\ 5.71981 & 350. & 0.232704 & 0 & 8 & 5 \\ 6.10628 & 350. & 0.578677 & 0 & 0 & 0 \\ 6.18357 & 566.667 & 0.51172 & 0 & 2 & 1 \\ \end{array} \right)$$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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