Using end as a variable to access parts of array

조회 수: 132 (최근 30일)
Shane Sullivan
Shane Sullivan 2017년 9월 21일
답변: 思诚 2025년 5월 6일
I need to access parts of the array, sometimes from 1:n, and sometimes from 1:end. However, as the program progresses, the length to the end will change. This math is going to be called over and over many many times, and I would like to avoid overhead of finding the length of the array every-time to input that as 'n'. So ideally I would like to be able to use 'end' as a variable. The following does not work
a = 1:10;
b = 'end';
a(5:end) results in Error: The end operator must be used within an array index expression.
Nor can I do something simple like b = end;. Anyone have any ideas how I can do this?
  댓글 수: 2
Cam Salzberger
Cam Salzberger 2017년 9월 21일
Running this code works for me. Is there anything else in your workspace that could be causing this to fail? Or a function called "a" or "end"?
Using "end" will inherently require checking the length of the array under the hood. However, you are correct that it is likely to be faster to do:
a(1:end)
than:
a(1:numel(a))
However, if you are doing 1:end, could you not just do:
a(:)
or just
a
Of course, that wouldn't work with replacing a(5:end).
I'm a little confused about why this is a big concern though. Can you give us more detail on your program logic flow, what your variable sizes are, and how they will change?
-Cam
Cam Salzberger
Cam Salzberger 2017년 9월 21일
One thing I can suggest is to profile your code as you run it. Try to see if it's really the calls to numel or length that are slowing down your code. Micro-optimization doesn't help if there are larger problems.

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

답변 (3개)

Star Strider
Star Strider 2017년 9월 21일
Using end should work. (I don’t know the MATLAB version when it first indicated the last element in a vector.)
These are equivalent (for a row vector):
Result1 = a(5:end);
Result2 = a(5:numel(a));
Result3 = a(5:length(a));
Result4 = a(5:size(a,2));
For column vectors, or matrices, use the size (link) function. See the documentation for details.
  댓글 수: 2
Shane Sullivan
Shane Sullivan 2017년 9월 21일
The point is, I can not use 'end'. I want my program depending on certain conditions to decide to go to end; but I want to avoid calculating what that end length/size is because I am doing this a crazy large # of times and it is slowing things down. So I am looking for someway possible to use the "end" as a variable essentially.
Star Strider
Star Strider 2017년 9월 21일
What you want to do and the reason you do not want to use end are not exactly clear. I doubt that using end is slowing your code.
If you want to avoid addressing a vector beyond its length (and throwing an error), something like this will work:
First = 3;
Last = 7;
Out = a(First : min(Last, length(a)));

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


KL
KL 2017년 9월 21일
편집: KL 2017년 9월 21일
Why do you want to do this? why not just use the length(a),
a(5:length(a))
  댓글 수: 3
Guillaume
Guillaume 2017년 9월 21일
Note that end is not equivalent to length(a). end is equivalent to numel(a) which will produce vastly different results on anything but vectors.
So:
a(5:numel(a))
KL
KL 2017년 9월 21일
I agree. I had only considered row vectors but in case of multiple manydimension matrices, why not put them all in a cell array? store their sizes in another cell array beforehand so you could save some time later in your loops/whatever.
C = {rand(2,4);rand(123,56);rand(100,43)};
C_sizes = cellfun(@size,C,'UniformOutput',false);
for k=1:100000
A = cellfun(@(a,b) a(1:b(1),2:b(2)),C,C_sizes,'UniformOutput',false);
%or anything else
end

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


思诚
思诚 2025년 5월 6일
This would work if you really want to call customized end at anywhere (outside subsref) you want.
feval("end",array,k,n) would call end as the end is placed on the kth place with total n subscripts in a subsref.
array: the array to call end on
k: index position. In your case it would be 1
n: total index number. In your case it would be 1
For example:
>>> feval("end",[1,2,3;4,5,6],1,2) % (:,1:end) end=2
ans =
2
>>> feval("end",[1,2,3;4,5,6],1,1) % (1:end) end=6
ans =
6

카테고리

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