フィルターのクリア

Taking the last value of a matrix or vector

2,012 ビュー (過去 30 日間)
Villanova
Villanova 2011 年 9 月 9 日
コメント済み: Voss 2024 年 6 月 25 日
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

採用された回答

Walter Roberson
Walter Roberson 2011 年 9 月 9 日
v(end)
or
v(length(v))
  4 件のコメント
Marcus
Marcus 2019 年 2 月 17 日
In MATLAB R2015a, end is faster than length():
>> x = rand(1, 100);
>> tic; for i=1:1e6; y=x(end); clear('y'); end; toc
Elapsed time is 5.475081 seconds.
>> tic; for i=1:1e6; y=x(length(x)); clear('y'); end; toc
Elapsed time is 5.705095 seconds.
Sangbok Lee
Sangbok Lee 2021 年 10 月 17 日
R2021a
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.

サインインしてコメントする。

その他の回答 (1 件)

Korosh Agha Mohammad Ghasemi
Korosh Agha Mohammad Ghasemi 2024 年 6 月 25 日
移動済み: Voss 2024 年 6 月 25 日
To access the last element of a vector in MATLAB regardless of its length, you can use the end keyword. This allows you to dynamically reference the last element without having to specify the exact index.
Here’s an example demonstrating how to use the end keyword:
% Example vector
v = [7, 8, 8, 2, 5, 6];
% Access the last element
last_element = v(end);
% Display the last element
disp('The last element is:');
disp(last_element);
% Using the last element somewhere else in your code
result = last_element + 10; % Example operation
disp('Result of operation with the last element:');
disp(result);
In this example, v(end) retrieves the last element of the vector v regardless of its length. You can then use last_element elsewhere in your code.
If you need to perform operations on the last element in multiple parts of your code, you can encapsulate the logic in a function. Here’s an example:
% Function to get the last element of a vector
function last_element = getLastElement(vector)
last_element = vector(end);
end
% Example usage
v = [7, 8, 8, 2, 5, 6];
last_element = getLastElement(v);
% Display the last element
disp('The last element is:');
disp(last_element);
% Using the last element somewhere else in your code
result = last_element + 10; % Example operation
disp('Result of operation with the last element:');
disp(result);
By using the end keyword, you can write more flexible and maintainable code that adapts to changes in the size of your vectors.
  1 件のコメント
Voss
Voss 2024 年 6 月 25 日
"regardless of its length"
Not true. The length must be greater than zero to use end. Here's an example demonstrating the problem:
v = [];
v(end)
Array indices must be positive integers or logical values.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by