Lecturer

End as a subscript   
To access the last element of a matrix along a given dimension, use  
end as a subscript. This allows you to go to the final element without  
knowing in advance how big the matrix is. For example:  

 

 

>> q = 4:10    
q=   
4   5   6   7   8   9   10    

 

 

>> q(end)   
ans = 10  

 

 
>> q(end-4:end)    
ans = 6   7   8   9   10  

 

 
>> q(end-2:end)   
ans = 8   9   10  

 

 
This technique works for two-dimensional matrices as well:   
>> q = [1 2 3;4 5 6;7 8 9]   
q=   
1 2 3   
4 5 6   
7 8 9