How do I tell Matlab a loop index is an integer?

19 visualizaciones (últimos 30 días)
I am perplexed by the fact that niether loop indeces or the number 3 are considered integers.
The script (called WhyNotIntegers)
for i = 1:3
idivide(i,3,'round')
end
produces these errors when run:
Error using idivide>idivide_check (line 66)
At least one argument must belong to an integer class.
Error in idivide (line 42)
idivide_check(a,b);
Error in WhyNotIntegers (line 2)
idivide(i,3,'round')
Error in run (line 96)
evalin('caller', [script ';']);

Respuesta aceptada

Image Analyst
Image Analyst el 22 de Feb. de 2019
They're doubles that just happen to have integer values. Convert them to integers since that's what idivide wants. Try this:
for k = 1:3
idivide(int32(k), int32(3),'round')
end
  1 comentario
Alexander MacFarlane IV
Alexander MacFarlane IV el 22 de Feb. de 2019
Thanks to both of you for the fast answers.
Stephan got there first, but this told me exactly what I needed to do.
Alex

Iniciar sesión para comentar.

Más respuestas (1)

Stephan
Stephan el 22 de Feb. de 2019
Editada: Stephan el 22 de Feb. de 2019
Hi,
by default numbers in Matlab are double. cast them to integer:
int32(i)
Read here:
Best regards
Stephan

Categorías

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

Etiquetas

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by