# How to approximate value of array according to specific ratio/ Error percentage

1 view (last 30 days)
Mohamed Jamal on 19 Jul 2020
Edited: Bruno Luong on 19 Jul 2020
Hi guys, I have an array y=[0.8 3.7 -0.8 -2.2 5.173 4.133] , I want to do in matlab approximations to my number as this :
if my number residue is bigger than 0.6 then map/add it to 1 or -1 (it depends to the sign of the number), for instance in my example:
0.8 is bigger than 0.6 so I want to approximate it to 1.
3.7 , the residue is 0.7 so it's bigger than 0.6 so I approximate 3.7 to 4 because the residue 0.7 (3.7-3=residue) is bigger than 0.6 so I add one to the complete number.
-0.8 is bigger than 0.6 in abs , mean abs(-0.8)>0.6 so I approimate it to -1
-2.2 the residue is 0.2 <0.6 so I approximate it to 2 and not to 3 because the residue (abs(-2.2)-2 = 0.2) is smaller than 0.6 so I'm not adding one to the complete number ..
4.133 , the residue is 0.133<0.6 so I approximate it to 4 and not adding one to complete number (4) because the residue 0.133 is smaller than 0.6 ..
etc ..
the rule is : whenver the residue of the number(modulo of the number) bigger than 0.6 then we are rounding up the number(rounding up the current value of the array), otherwise we are rounding down the number(rounding down the current value of the array).

Bruno Luong on 19 Jul 2020
round(y)
Mohamed Jamal on 19 Jul 2020
Edited: Mohamed Jamal on 19 Jul 2020
but if I want for instance to round according to residue 0.7 , it means if residue >0.7 then rounding up, otherwise rounding down ! .. how can I do that in matlab?
I also edited my question because Im asking how to approximate according to specific residue/ratio/error ratio , not specifically 0.5
Bruno Luong on 19 Jul 2020
Edited: Bruno Luong on 19 Jul 2020
round(y-(0.7-0.5)) % 0.7 here is the desired threshold, do not change 0.5
Alternatively
floor(y+(1-0.7))
or the simplest
ceil(y-0.7)