开发者

What is the shortest way of getting the last N items of an array?

开发者 https://www.devze.com 2023-01-27 12:49 出处:网络
I wrote array = linspa开发者_运维知识库ce(0, 1); sliceSize = 10; sliceBegin = 1 + length(array) - sliceSize;

I wrote

array = linspa开发者_运维知识库ce(0, 1);
sliceSize = 10;
sliceBegin = 1 + length(array) - sliceSize;
slice = array(sliceBegin: length(array));

that's too verbose. How to make it shorter?


a = rand(100,1);    %# vector
a(end-5+1:end)      %# last five elements
0

精彩评论

暂无评论...
验证码 换一张
取 消