码迷,mamicode.com
首页 > 其他好文 > 详细

Matlab learning summary

时间:2019-08-26 21:00:27      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:line   输出   rate   declared   collect   tla   提前   images   create   

1.Overload your functions by having variable number of input and output argumernt.Not only can we overload functions also operators.

我们可以通过不同的输入输出来重载函数,当然我们还可以重载运算符。(see varargin,varargout,nargin,nargout)

varargin :

Allows any number of arguments to a function. The variable varargin is a cell array containing the optional arguments to the function. varargin must be declared as the last input argument and collects all the inputs from that point onwards. In the declaration, varargin must be lowercase (i.e., varargin).

允许函数输入任意数量的参数,varargin实际上是一个cell,包含着函数的可选参数。varargin参数必须在参数列表的最右面,在声明时,varargin必须小写!

 

For example, the function,

例如,函数:

function myplot(x,varargin)
plot(x,varargin{:})

collects all the inputs starting with the second input into the variable "varargin". MYPLOT uses the comma-separated list syntax varargin{:} to pass the optional parameters to plot.

这个函数把从第二个输入的参数全部收集进变量"varargin"

 

The call,

myplot(sin(0:.1:1),‘color‘,[.5 .7 .3],‘linestyle‘,‘:‘)

results in varargin being a 1-by-4 cell array containing the
values ‘color‘, [.5 .7 .3], ‘linestyle‘, and ‘:‘.

varargout:ariable length output argument list.

Allows any number of output arguments from a function. The variable varargout is a cell array containing the optional output arguments from the function. varargout must be declared as the last output argument and must contain all the outputs after that point onwards. In the declaration, varargout must be lowercase (i.e., varargout).

 

允许函数任意数量的输出,varargout和varargin的数据类型一样,是一个cell,包含了函数的可选的任意输出,同样varargout也必须是在输出的左后一个参数,在声明时,varargout必须小写!

varargout is not initialized when the function is invoked. You must create it before your function returns. Use NARGOUT to determine the number of outputs to produce.

varargout直至函数被调用时才被初始化,你必须在你的函数返回前创造他,使用NARGOUT来决定要产生的输出(总)的数目

For example, the function,
例:
function [s,varargout] = mysize(x)
nout = max(nargout,1)-1;
s = size(x);
for i=1:nout, varargout(i) = {s(i)};%提前创造varargin(remember varargout is a cell)

end

returns the size vector and optionally individual sizes. So,


[s,rows,cols] = mysize(rand(4,5));

returns s = [4 5], rows = 4, cols = 5.

 

2.Visualizing matrices 的几个函数

a.colormap    b.surf    c.contour   d.colorbar    e.imagesc

(all thiese are built-inn functions)

3.计算程序运行的时间;

tic ....toc1...toc2...此外还有for more complicated situation >>profile on;>>profile viewer

 

Matlab learning summary

标签:line   输出   rate   declared   collect   tla   提前   images   create   

原文地址:https://www.cnblogs.com/saurywb/p/11414613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!