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

 채택된 답변

David Fletcher
David Fletcher 2018년 4월 3일

4 개 추천

W(end-1)

댓글 수: 2

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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

제품

태그

질문:

2018년 4월 3일

댓글:

DGM
2021년 8월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by