필터 지우기
필터 지우기

Take last n elements in vector

조회 수: 200 (최근 30일)
Tiffany
Tiffany 2022년 10월 19일
댓글: Tiffany 2022년 10월 20일
Hello,
I have an input variable that can get changed by the user. I want to index from the end of a vector to essentially end-input
e.g., in pseudocode
a = 1:10;
a = [1,2,3,4,5,6,7,8,9,10]
user_input = 3
correct_answer = end-user_input
correct_answer = 8,9,10
This is what I have, and it works, but is there a cleaner way?
range_a = a((length(a)-user_input):end-1);
Thank you!

채택된 답변

Eric Delgado
Eric Delgado 2022년 10월 19일
Try this...
a = [1,2,3,4,5,6,7,8,9,10];
N = 3; % user_input
range_a = a(end-N+1:end)
range_a = 1×3
8 9 10
  댓글 수: 1
Tiffany
Tiffany 2022년 10월 20일
This worked. Thanks so much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by