Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB expression column indexing

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!

like image 880
yassin Avatar asked Apr 28 '10 09:04

yassin


People also ask

How do I index a specific column in MATLAB?

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.

How do you do logical indexing in MATLAB?

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.

What is indexing in MATLAB?

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.

How do you find the index of a value in MATLAB?

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.


1 Answers

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
like image 115
Jason S Avatar answered Sep 21 '22 19:09

Jason S