rounding elements in array to nearest 0.25
123 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Srinivas
el 25 de Ag. de 2011
Comentada: Matthias Schneider
el 4 de Dic. de 2023
i need to round off some numbers to nearest quarter , with an option to round up or round down to nearest quarter
for e.g. For an array for (5,1) with following values
1373.698
1385.024
1394.82
1400.436
1396.532
output for rounding up to closest 0.25 will result in
1373.75
1385.25
1395.00
1400.50
1396.75
output for rounding down to closest 0.25 will be
1373.50
1385.00
1394.75
1400.25
1396.50
I am not sure how to create such a function which gives this output, please help .
1 comentario
Respuesta aceptada
Friedrich
el 25 de Ag. de 2011
Hi,
I think you are looking for:
in = [1373.698
1385.024
1394.82
1400.436
1396.532];
%round up
up = floor(in) + ceil( (in-floor(in))/0.25) * 0.25
%round down
down = floor(in) + floor( (in-floor(in))/0.25) * 0.25
2 comentarios
Más respuestas (2)
Walter Roberson
el 25 de Ag. de 2011
The previous answers look more complex than needed
%round up
up = ceil(in * 4) / 4;
%round down
down = floor(in * 4) / 4;
2 comentarios
SL
el 25 de Oct. de 2016
Editada: SL
el 25 de Oct. de 2016
Can you make this a single function, please? - - It would be then a complete answer to the question, at least for me. I extended my case here https://se.mathworks.com/matlabcentral/answers/309097-how-to-round-up-to-closest-0-0-or-0-5-in-array
Anders Tagmose Lundsfryd
el 17 de Mzo. de 2021
Editada: Anders Tagmose Lundsfryd
el 6 de Abr. de 2021
If you want to round to the nearest 0.25 both up and down, then do:
%roudn to nearest 0.25
updown = round(in * 4)/4;
Ver también
Categorías
Más información sobre Data Distribution Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!