# When you type m(1) or m(10) in the command window, what is happening?

2 visualizaciones (últimos 30 días)
ssmith el 16 de Nov. de 2021
Comentada: John D'Errico el 16 de Nov. de 2021
m(1)
ans =
-3
m(10)
ans =
0.6000
m(0)
Array indices must be positive integers or logical
values.
m(0) does not work because it needs to have a positive integer or logical value but what is happening when you do use a correct number?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

Awais Saeed el 16 de Nov. de 2021
Let's say you have an array of 10 elements
arr = [1 2 5 4 7 0 3 6 1 9];
To access first element, you use
arr(1)
ans = 1
to access 5th element, you use
arr(5)
ans = 7
So your m(1) and m(10) means to access first and 10th element of array m. Same goes for a matrix.
##### 2 comentariosMostrar NingunoOcultar Ninguno
ssmith el 16 de Nov. de 2021
@Awais Saeed and m(0) does not work because there is no 0 element in a matrix?
John D'Errico el 16 de Nov. de 2021
@ssmith: It is not that the matrix does not contain a zero in it, if that is what you are asking.
MATLAB uses a 1-based index origin. The first element is element number 1. Yes, other languages use a zero origin. But that is just an arbitrary choice. Regardless, m(0) fails, because the zero'th element does not exist.

Iniciar sesión para comentar.

### Categorías

Más información sobre Matrices and Arrays 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!

Translated by