"end-1" as parameter

조회 수: 71 (최근 30일)
Chen Lin
Chen Lin 2016년 9월 9일
답변: Chen Lin 2016년 9월 9일
I have a question on MATLAB Onramp Chapter 5.1 Indexing into Arrays, Task 3:
Under what "real" scenario would I use the "end-1" command to find a value?
In the given example of MATLAB Onramp, the task is to find the value in the 6th row and 3rd column. I understand that I can use the keyword "end" to either find a row or column index to reference of the last element, which is quick and straightforward in a dataset, but why would I use the "end-1" as parameter? Doesn't it make it too complicated to find an "unknown" value?
Any further explanation on this is much appreciated.
  댓글 수: 2
John D'Errico
John D'Errico 2016년 9월 9일
It would help if you provide a link. Without that or a copy of the code/question, it is difficult to know what you are talking about.
Steven Lord
Steven Lord 2016년 9월 9일
I believe that's part of MATLAB Academy.

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

채택된 답변

Guillaume
Guillaume 2016년 9월 9일
Well, end-1 is not an unknown location. It's the element before the last. There are plenty of scenarios where you'd want the nth element before the end, so end-n is very useful. e.g.:
  • Implementing a LIFO (Last In First Out) stack. When you want to pop the last element from the stack:
function [stack, element] = pop(stack)
element = stack(end); %get last element
stack = stack(1:end-1); %return the stack without the last element
end
  • When you want to remove the border around an image:
function croppedimage = cropborder(sourceimage, bordersize)
croppedimage = sourceimage(bordersize+1:end-bordersize, bordersize+1:end-bordersize, :);
end
  • split a vector into two:
function [firsthalf, secondhalf] = splitmiddle(vector)
firsthalf = vector(1:ceil(end/2));
secondhalf = vector(ceil(end/2)+1:end);
end
  • etc.

추가 답변 (2개)

Steven Lord
Steven Lord 2016년 9월 9일
Let's say I wanted to add each element of a vector to the next element. I could do this with a for loop, or I could use a vectorized approach. But I can't add the last element of the vector with the next element -- there isn't a next element! So I add every element of the vector EXCEPT the last (which I retrieve using the index vector 1:end-1) to the one just after that (which I retrieve using the index vector (1:end-1)+1 or 2:end.)
x = 1:10;
y = x(1:end-1) + x(2:end);
We introduced in release R2016a a function named movsum that you could use instead to compute y, but the indexing approach works back probably to Cleve's original Fortran version of MATLAB.

Chen Lin
Chen Lin 2016년 9월 9일
Hi all, thanks for the quick responses.
And John was right, so is Steven's assumption. The original question came from MATLAB Onramp Chapter 5.1, Task 3 (https://matlabacademy.mathworks.com/R2016a/portal.html?course=gettingstarted#chapter=5&lesson=1&section=1).
It was given: _>> load datafile >> data
data =
3.0000 0.5300 4.0753 NaN
18.0000 1.7800 6.6678 2.1328
19.0000 0.8600 1.5177 3.6852
20.0000 1.6000 3.6375 8.5389
21.0000 3.0000 4.7243 10.1570
23.0000 6.1100 9.0698 2.8739
38.0000 2.5400 5.3002 4.4508_
Task 3: _Info: Note that you can use arithmetic with the keyword end. For example:
>> x = A(end-1,end-2)_
Try creating a scalar variable p that contains the value in the second to last (end-1) row and 3rd column of data.
I didn't understand the meaning of "the second to last" (end-1) row first, but I figured out now. It was easy.
And thanks to Guillaume's practical examples. Perfect gents. Much appreciated!

커뮤니티

더 많은 답변 보기:  원격 교육 커뮤니티

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by