I was just wondering if there is a way to index using end
before knowing a vector's size? It should work for arrays with different sizes. Like this:
subvector = (2:end) % illegal use of end
A=[1 2 3];
B=[4 5 6 7];
A(subvector) % should be 2 3
B(subvector) % should be 5 6 7
The most common way is to explicitly specify the indices of the elements. For example, to access a single element of a matrix, specify the row number followed by the column number of the element. e is the element in the 3,2 position (third row, second column) of A .
In most programming languages, the first element of an array is element 0. In MATLAB, indexes start at 1. Arrays can be sliced by using another array as an index.
MATLAB does not allow an index of zero into an array unless you are performing logical indexing using a vector including a logical 0 and want to ignore the corresponding element of the array into which you are indexing.
You can set up an anonymous function to act in a similar way
f_end = @(v) v(2:end);
A = [1 2 3];
B = [4 5 6 7];
f_end( A ); % = [2 3];
f_end( B ); % = [5 6 7];
I think this is the only way you could do it, since you can't set up an indexing array without knowing the end
index.
Without indexing or usage of end
, one can remove the first element:
f_end = A;
f_end[1] = [];
As a function:
function x = f_end(y, n)
x = y;
x[1:n]=[]; % deletes the first n elements
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With