the integer part of the division

487 visualizaciones (últimos 30 días)
vaggelis vaggelakis
vaggelis vaggelakis el 4 de Oct. de 2012
Comentada: Royi Avital el 9 de Abr. de 2021
hello everyone
how do i get the integer part of the output of a division i.e. 23/5=4 (and the remainder is 3)
  2 comentarios
Shashank Agarwal
Shashank Agarwal el 15 de Nov. de 2016
floor(23/5)
Walter Roberson
Walter Roberson el 16 de Nov. de 2016

Iniciar sesión para comentar.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 4 de Oct. de 2012
Editada: Azzi Abdelmalek el 5 de Oct. de 2012
fix(23/5) % integer part
rem(23,5) % remainder
%or
mod(23,5)
  3 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 5 de Oct. de 2012
It's fixed with fix
Royi Avital
Royi Avital el 9 de Abr. de 2021
It works only for Float64 and Float32.
For integer types use idivide.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 5 de Oct. de 2012
Consider -23/5 (that is, the case of negative numbers.) Should that be -4 times 5 and remainder -3, or should it be -5 times 5 and remainder 2 ?
If you want the -4 version so that abs() of the "integer part" of -23/5 and 23/5 are the same, then use fix()
If you want the -5 version so that the remainder is always non-negative, then use floor()
You can use either fix() or floor() if you only have positive values, with floor() being preferred (more efficient, mathematically clearer)
Do not use round() even if you have only positive values. round(23/5) is 5, not 4.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by