Round values to specific number
121 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Amit Ifrach
el 7 de Feb. de 2023
Respondida: Amit Ifrach
el 9 de Feb. de 2023
לק"י
Hi,
I try to discretize vector A by vector edges. for example:
edges = [0:10^5:max(A)];
dsc = discretize(A, edges);
The problem is that the edges vectorcreated rounds down the value to 290000:
[0:10^5:max(aca2_100123pacd3)]
ans =
Columns 1 through 12
.....
Columns 25 through 30
2400000 2500000 2600000 2700000 2800000 2900000
I need the range of edges to be bigger than the max(A) so the dsc of max(A) will give a number and not a NaN output.
Any ideas how could it be done easily withouth writing about 3-5 lines?
I saw the round to 100 at the matlab round page, but want something that will suite not only 100, but any desiried number:
Round the number 863178137 to the nearest multiple of 100.
round(863178137,-2)
ans = 863178100
Thanks!
0 comentarios
Respuesta aceptada
Voss
el 7 de Feb. de 2023
"want something that will suit not only 100, but any desiried number"
You can do round(val./d).*d where val is the original number(s) and d is the number(s) you want the result(s) to be a multiple of.
Examples:
% round 1100 to the nearest multiple of 3:
val = 1100;
d = 3;
result = round(val./d).*d
% round 102 to the nearest multiple of 7:
val = 102;
d = 7;
result = round(val./d).*d
% round 10 to the nearest multiple of 3.7:
val = 10;
d = 3.7;
result = round(val./d).*d
% round 10:10:100 to the nearest multiple of 9:
val = 10:10:100;
d = 9;
result = round(val./d).*d
% round 11 to the nearest multiple of 7, round 12 to the nearest multiple of 5, round 13 to the nearest multiple of 6:
val = [11 12 13];
d = [7 5 6];
result = round(val./d).*d
0 comentarios
Más respuestas (2)
Jan
el 7 de Feb. de 2023
The colon operator creates a vector from the 1st argument in steps of the 2nd argument until the 3rd argument, but it does not include the last value necessarily. If you want it, include it explicitly:
maxA = max(A);
edges = [0:1e5:maxA, maxA];
But is maxA is divisable by the stepsize, it appears twice in the vector. If you want to avoid this, write a simple function to solve this:
function v = ColonInclude(a,b,c)
v = a:b:c;
if v(end) < c
v(end + 1) = c;
end
end
Matlab is a programming language. Why do you want to use it "withouth writing about 3-5 lines"?
2 comentarios
Bjorn Gustavsson
el 7 de Feb. de 2023
Should be possible to golf this a bit:
maxA = max(A);
edges = unique([0:1e5:maxA, maxA]);
Steven Lord
el 7 de Feb. de 2023
Another way to do this is to add (b-1) to the upper limit. If c is exactly a multiple of b away from a that's not far enough to get c+b added to the vector but if it's any larger you get the extra point.
I'm assuming everything here is an integer value. Otherwise you might need to get tricky and add b-someVerySmallNumber to the upper limit.
a = 0;
b = 10;
c = 121;
v = a:b:(c+b-1)
c = 120;
v = a:b:(c+b-1)
c = 129;
v = a:b:(c+b-1)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!