I have an expression that gives a matrix and I want to access an element, without creating a temporary variable, something like this cov(M)(1,1)
. How can I do it?
Thanks!
To access elements in a range of rows or columns, use the colon . For example, access the elements in the first through third row and the second through fourth column of A . An alternative way to compute r is to use the keyword end to specify the second column through the last column.
In logical indexing, you use a single, logical array for the matrix subscript. MATLAB extracts the matrix elements corresponding to the nonzero values of the logical array. The output is always in the form of a column vector. For example, A(A > 12) extracts all the elements of A that are greater than 12.
Indexing into a matrix is a means of selecting a subset of elements from the matrix. MATLAB® has several indexing styles that are not only powerful and flexible, but also readable and expressive. Indexing is a key to the effectiveness of MATLAB at capturing matrix-oriented ideas in understandable computer programs.
In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indices and the element from the array. The find() function returns a vector containing the data.
It's possible using anonymous functions:
>> f11 = @(M) M(1,1);
>> M = [1 2; 9 4];
>> cov(M)
ans =
32 8
8 2
>> f11(cov(M))
ans =
32
Or for the purists, here it is with no intermediate variables at all:
>> feval(@(M) M(1,1), cov(M))
ans =
32
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