6
$\begingroup$

I have a list of mesures that each vector is like {0,1,-2,259} but it should be like {0.1,-2.259} So I did a rule, like list/. {a_,b_,c_,d_}->{a.b,c.d} but for the positives number it works fine! but for vector like this one I get {0.1,(-2).239} how can I fix this?

$\endgroup$
  • $\begingroup$ No, is like i put it $\endgroup$ – Kafkarudo Sep 12 '14 at 20:55
  • $\begingroup$ I exported the data from Matlab as csv, but my decimal symbol here is a , so... $\endgroup$ – Kafkarudo Sep 12 '14 at 20:59
  • $\begingroup$ Sounds like you are after a kludge solution to a problem that ought to be fixed on the matlab side. $\endgroup$ – george2079 Sep 12 '14 at 21:14
  • 1
    $\begingroup$ Please note that the . in your rule is shorthand for Dot and not a decimal point! You almost certainly don't want the inner product of the whole part and fractional part! $\endgroup$ – evanb Sep 12 '14 at 22:06
8
$\begingroup$

May be I'm overthinking this one.

list = {0, 1, -2, 259};
f[x_, y_] :=  x + (2 Boole@NonNegative[x] - 1 ) y/10^IntegerLength[y]//N ;
list /. {a_, b_, c_, d_} -> {f[a, b], f[c, d]}

(* {0.1, -2.259} *)
$\endgroup$
  • 2
    $\begingroup$ just beat me to it, yours is the same as mine (essentially):toDecimal[a_, b_] := N[a + (-1)^Sign[a] b 10^(-IntegerLength[b])]; list /. {a_, b_, c_, d_} :> {toDecimal[a, b], toDecimal[c, d]} (* {0.1, -2.259} *) $\endgroup$ – chuy Sep 12 '14 at 21:12
  • $\begingroup$ @chuy Yup. (-1)^Sign[a] in definitively nicer. $\endgroup$ – Dr. belisarius Sep 12 '14 at 21:15
  • $\begingroup$ Thanks, this work, but can't understand how...it will take me more than an hour to understand it :) $\endgroup$ – Kafkarudo Sep 12 '14 at 22:27
  • $\begingroup$ @Kafkarudo Take the code in the comment above by chuy. It does the same thing and I believe it's easier to understand $\endgroup$ – Dr. belisarius Sep 12 '14 at 22:39
6
$\begingroup$

Similar to Alan's answer but a bit more direct:

fix = ToExpression @ ToString @ Row[#, "."] &;

in = {0, 1, -2, 259};

fix /@ Partition[in, 2]
{0.1, -2.259}
$\endgroup$
  • $\begingroup$ Why not Row[#,"."]&/@Partition[in,2] ? $\endgroup$ – Kafkarudo Sep 13 '14 at 22:13
  • $\begingroup$ @Kafkarudo I assumed that you wanted numeric values as output rather than merely expressions that look like numeric values. If not then yes, you may drop ToString and ToExpression. $\endgroup$ – Mr.Wizard Sep 14 '14 at 0:19
3
$\begingroup$

The first output may look ok but it's not actually a number. See if this does what you want:

test = {0, 1, -2, 259}
combine[{a_, b_}] := ToExpression[ToString[StringForm["``.``", a, b]]]
Map[combine, ArrayReshape[test, {Length[test]/2, 2}]]
$\endgroup$
  • $\begingroup$ similarly: ToExpression[StringJoin[Riffle[ ToString /@ #, "."]]] & /@ Partition[{0, 1, -2, 259}, 2] $\endgroup$ – george2079 Sep 12 '14 at 21:19
1
$\begingroup$
ClearAll[f];
f = With[{left = FromDigits /@ IntegerDigits /@ #[[;; ;; 2]],
          right = N /@ FromDigits /@ ({#, 0} & /@ IntegerDigits /@ #[[2 ;; ;; 2]]),
          sign = Sign /@ #[[;; ;; 2]] /. 0 -> 1}, sign (left + right)] &;

list = {0, 1, -2, 259};
list2 = {0, 1, -2, 259, 3, 141592653};

Grid[{#, f@#} & /@ {list, list2} , Dividers -> All]

enter image description here

$\endgroup$
0
$\begingroup$
f = With[{p = Length[IntegerDigits@#2]}, 
    N[#1 + (2 UnitStep[#1] - 1) #2 10^(-p)]] &;
g = f @@@ Partition[#, 2] &;

Test case:

test = {{0, 1, -2, 259}, {-1, 2, 3, 124}, {-2, 34, -3, 45}, {1, 0, 3, 
   4}}
Grid[{#, "\[RightArrow]", g@#} & /@ test]

enter image description here

Now strings would be required to deal with e.g.

test2 = {{"-1", "04", "0", "04"}, {"-1", "234", "2", "32"}, {"0", 
    "2345", "2", "003"}};

In this case:

h[u_] := N[
     ToExpression[{#1, #2}].{1, (2 UnitStep[ToExpression@#1] - 
          1) 10^-StringLength[#2]}] & @@@ Partition[u, 2];

So,

Grid[{#, "\[RightArrow]", h@#} & /@ test2]

enter image description here

$\endgroup$
-1
$\begingroup$
list = {0, 1, -2, 259};
list /. {a_, b_, c_, d_} :> {a.b, If[Sign[c] == -1, -(-c).d, c.d]}
(*{0.1, -2.259}*)

ToExpression[
 StringReplace[
  ToString /@ 
   Partition[{0, 1, -2, 259}, 2], {{"{" | "}" | (Whitespace )} :> "", 
   "," -> "."}]]

(*{0.1, -2.259}*)
$\endgroup$
  • $\begingroup$ I am not the one who down-voted, but I see why: this does not work. Look at the FullForm -- you have Dot in the result, not a Real number. $\endgroup$ – Mr.Wizard Sep 13 '14 at 3:05
  • $\begingroup$ @Mr.Wizard I know. I have looked at it previously after some one down-voted. thanks. $\endgroup$ – Algohi Sep 13 '14 at 3:16

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.