Anonymous function:
Anonymous function is not stored in the program file, but with the type of data is a function of variable function_handle related. Anonymous functions can accept input and return output, just like standard functions. However, they may contain only one executable statement.
For example, create an anonymous function is used to calculate the number of square handle:
sqr = @(x) x.^2;
And pre-allocated to the quantization (where speed):
- Vectorization: matlab to make higher-speed work, the .m file algorithm to quantify, other languages use a for loop, Matlab available vector or matrix operations.
Example: The original algorithm:
x=0.01;
for k=1:1001
and (k) = log10 (x);
x=x+0.01;
end
Vectorization:
x=0.01:0.01:1001;
y = log10 (x);
- Pre-allocation: the code can not be quantified, for circulation to speed up the pre-allocated by by
Example:
r=zeros(1,32);
for n=1:32
r(n)=rank(magic(r));
end
Function Function:
Kind of scalar variables in a nonlinear function, a function called the function argument, 1️ a function name from the function variables. Seeking zero, optimization, quadrature and ordinary differential equations.
Example (simple implementation hump Functions Function):
Has a built-in MATLAB function is humps, the function is a special case of the following functions:
When q = 0.3, r = 0.9 and s = 6, it is the function humps.
a=0:0.002:1;
b=humps(a);
plot(a,b)
function b=humps
b=1./((x-.3).^2+.01)+1./((x-.9).^2+.04)-6;
end
Graphical results as:
Command line window to perform:
>> p = (@ humps, 0.5)% and find a local minimum value at this time is fminsearch x
% Of the first parameter function handle, as the second parameter. Approximations
p =
0.6370
Call the function in the function
>> humps (p)% find the lowest value
years =
11.2528
P-code file:
.M a file is first invoked, matlab will parsing the document, and generates corresponding internal pseudocode (Psecode P code) files are stored in the memory
When the file is called again, only called P-code file, not the original .m file parsing. And the file will be converted to matlab along with all the files in the function call code file P
P-code file .m file has the same file name extension .p, p code file speed higher than the original pattern file.
If there are files with the same name and code p .m files, matlab code files will first call p.
P-code file related commands:
inmem: a list of all P-code file name
clear FunName: Clear p code in a file named FunName.p memory
clear functions: remove all the P-code files