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?
Asked
Active
Viewed 237 times
6
Kafkarudo
- 619
- 5
- 9
6 Answers
8
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} *)
Dr. belisarius
- 115,881
- 13
- 203
- 453
-
2just 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} *)
– chuy Sep 12 '14 at 21:12 -
-
Thanks, this work, but can't understand how...it will take me more than an hour to understand it :) – Kafkarudo Sep 12 '14 at 22:27
-
@Kafkarudo Take the code in the comment above by chuy. It does the same thing and I believe it's easier to understand – Dr. belisarius Sep 12 '14 at 22:39
6
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}
Mr.Wizard
- 271,378
- 34
- 587
- 1,371
-
-
@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
ToStringandToExpression. – Mr.Wizard Sep 14 '14 at 00:19
3
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}]]
Alan
- 13,686
- 19
- 38
-
similarly:
ToExpression[StringJoin[Riffle[ ToString /@ #, "."]]] & /@ Partition[{0, 1, -2, 259}, 2]– george2079 Sep 12 '14 at 21:19
1
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]

kglr
- 394,356
- 18
- 477
- 896
0
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]

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]

ubpdqn
- 60,617
- 3
- 59
- 148
-1
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}*)
Basheer Algohi
- 19,917
- 1
- 31
- 78
-
I am not the one who down-voted, but I see why: this does not work. Look at the
FullForm-- you haveDotin the result, not a Real number. – Mr.Wizard Sep 13 '14 at 03:05 -
@Mr.Wizard I know. I have looked at it previously after some one down-voted. thanks. – Basheer Algohi Sep 13 '14 at 03:16
.in your rule is shorthand forDotand not a decimal point! You almost certainly don't want the inner product of the whole part and fractional part! – evanb Sep 12 '14 at 22:06