Take last n elements in vector
138 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tiffany
el 19 de Oct. de 2022
Comentada: Tiffany
el 20 de Oct. de 2022
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!
0 comentarios
Respuesta aceptada
Eric Delgado
el 19 de Oct. de 2022
Try this...
a = [1,2,3,4,5,6,7,8,9,10];
N = 3; % user_input
range_a = a(end-N+1:end)
Más respuestas (0)
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!