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)
. Ho开发者_开发技巧w can I do it?
Thanks!
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
I have a function like this in my path:
getRegion = @(matrix, rows, cols) matrix(rows,cols);
So that I can then call:
getRegion(cov(M), 1, 1);
It would also work if you wanted a larger region:
getRegion(cov(M), 1:2, 2);
精彩评论