How can I find the second last value of a vector?

Is there a command that returns the second last value in a value. For example, in vector W = [1 2 3 4...n], W(end) returns n, how would I return the n-1 value? Thanks

 Respuesta aceptada

David Fletcher
David Fletcher el 3 de Abr. de 2018
W(end-1)

2 comentarios

I just tried this and matkab says "Array indices must be positive integers or logical values."
What size is your variable? If you are indexing along a dimension of length 1, then you will end up trying to access the zeroth subscript -- which is not valid.
A = 27;
A(1)
ans = 27
A(end)
ans = 27
A(end-1)
Array indices must be positive integers or logical values.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 3 de Abr. de 2018

Comentada:

DGM
el 22 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by