Taking the last value of a matrix or vector

4.065 views (last 30 days)
Hi. How can I take a last term of a vector - since my vector dimensions change -, and plug it in somewhere else in my code without keep changing the code?
for example right now I do v(6) to get the 6th term. How can I do it for nth term.... Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 9 Sep 2011
Sangbok Lee
Sangbok Lee on 17 Oct 2021
I've tried it several times.
"end" is always slightly faster than "length()"
I think "end" works the same way using index numbers.
>> x=rand(10000);
>> tic; for i=1:1e7; y=x(end); clear y; end; toc
Elapsed time is 21.722168 seconds.
>> tic; for i=1:1e7; y=x(length(x),length(x)); clear y; end; toc
Elapsed time is 22.073104 seconds.
>> tic; for i=1:1e7; y=x(10000,10000); clear y; end; toc
Elapsed time is 21.818949 seconds.

Sign in to comment.

More Answers (0)


Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by