# 我如何索引一个函数返回的MATLAB数组，而不先将它分配给一个局部variables？

` `M = magic(5); value = M(3,3);` `

` `value = magic(5)(3,3); value = (magic(5))(3,3);` `

` `value = magic(5)(3,3);` `

` `value = subsref(magic(5),struct('type','()','subs',{{3,3}}));` `

` `subindex = @(A,r,c) A(r,c); %# An anonymous function to index a matrix value = subindex(magic(5),3,3); %# Use the function to index the matrix` `

` `paren = @(x, varargin) x(varargin{:}); curly = @(x, varargin) x{varargin{:}};` `

`paren()`可以用在哪里

` `paren(magic(5), 3, 3);` `

` `ans = 16` `

` `>> builtin('_paren', magic(5), 3, 3) %# M(3,3) ans = 13` `

` `>> builtin('_brace', num2cell(magic(5)), 3, 3) %# C{3,3} ans = 13` `

## 更新：

` `a=rand(5); getfield(a,{1,2}) % etc` `

` `tmp = magic(3); myVar = tmp(3,3); clear tmp` `

` `subs=@(M,i,j) M(i,j); >> for nit=1:10;tic;subs(magic(100),1:10,1:10);tlap(nit)=toc;end;mean(tlap) ans = 0.0103 >> for nit=1:10,tic;M=magic(100); M(1:10,1:10);tlap(nit)=toc;end;mean(tlap) ans = 0.0101` `

` `function [ element ] = getElem( matrix, index1, index2 ) element = matrix(index1, index2); end` `

` `value = getElem(magic(5), 3, 3);` `

` `M = magic(5); %create value = M(3,3); % extract useful data clear M; %free memory` `

BUILTIN（…）与FEVAL（…）相同，只是它将调用函数的原始内置版本，即使存在重载的版本（为此，您不能超载BUILTIN）。

` `>> feval('_paren', magic(5), 3, 3) % M(3,3) ans = 13 >> feval('_brace', num2cell(magic(5)), 3, 3) % C{3,3} ans = 13` `

` `>> tic; for i=1:1e6, feval('_paren', magic(5), 3, 3); end; toc; Elapsed time is 49.904117 seconds. >> tic; for i=1:1e6, builtin('_paren', magic(5), 3, 3); end; toc; Elapsed time is 51.485339 seconds.` `