我想每个人都会同意MATLAB语言不漂亮或特别一致.但是不要紧!我们仍然必须用它来完成任务.
你最喜欢的技巧是什么?我们每个答案都有一个,所以如果他们同意,人们可以投票.另外,尝试用一个例子来说明你的答案.
Jas*_*ram 39
使用内置的分析器查看我的代码的热门部分:
profile on
% some lines of code
profile off
profile viewer
Run Code Online (Sandbox Code Playgroud)
或者只是使用内置的tic并toc获得快速时间:
tic;
% some lines of code
toc;
Run Code Online (Sandbox Code Playgroud)
sun*_*ica 31
使用逻辑数组直接提取满足特定条件的矩阵元素:
x = rand(1,50) .* 100;
xpart = x( x > 20 & x < 35);
Run Code Online (Sandbox Code Playgroud)
现在xpart只包含那些位于指定范围内的x元素.
Sco*_*e T 28
通过在帮助注释中添加"另请参阅"行,可以快速访问其他功能文档.首先,您必须在所有大写字母中包含函数的名称作为第一个注释行.做你平常的评论标题,然后再看看其他相关函数的逗号分隔列表.
function y = transmog(x)
%TRANSMOG Transmogrifies a matrix X using reverse orthogonal eigenvectors
%
% Usage:
% y = transmog(x)
%
% SEE ALSO
% UNTRANSMOG, TRANSMOG2
Run Code Online (Sandbox Code Playgroud)
当您在命令行中键入"help transmog"时,您将看到此注释标题中的所有注释,以及指向列出的其他函数的注释标题的超链接.
Jas*_*n S 20
匿名函数,原因如下:
quad和fminbnd函数作为参数非常有用.它在脚本中也很方便(.m文件不以函数头开头),因为与真正的函数不同,你不能包含子函数..
% quick functions
f = @(x) 3*x.^2 + 2*x + 7;
t = (0:0.001:1);
plot(t,f(t),t,f(2*t),t,f(3*t));
% closures (linfunc below is a function that returns a function,
% and the outer functions arguments are held for the lifetime
% of the returned function.
linfunc = @(m,b) @(x) m*x+b;
C2F = linfunc(9/5, 32);
F2C = linfunc(5/9, -32*5/9);
Run Code Online (Sandbox Code Playgroud)
Jas*_*n S 18
图表中公式的LaTeX模式:在最近的一个版本(R2006?)中,您,'Interpreter','latex'在函数调用结束时添加了其他参数,它将使用LaTeX渲染.这是一个例子:
t=(0:0.001:1);
plot(t,sin(2*pi*[t ; t+0.25]));
xlabel('t');
ylabel('$\hat{y}_k=sin 2\pi (t+{k \over 4})$','Interpreter','latex');
legend({'$\hat{y}_0$','$\hat{y}_1$'},'Interpreter','latex');
Run Code Online (Sandbox Code Playgroud)
不知道什么时候添加它,但它适用于文本(),title(),xlabel(),ylabel(),zlabel()甚至legend()函数中的R2006b.只需确保您使用的语法不明确(因此使用legend()时,您需要将字符串指定为单元格数组).
Sam*_*mil 17
使用xlim和ylim绘制垂直和水平线.例子:
在y = 10处画一条水平线:
line(xlim, [10 10])
在x = 5处绘制垂直线:
line([5 5], ylim)
Mat*_*att 16
这是一个简单的例子:
我发现逗号分隔列表语法对于构建函数调用非常有用:
% Build a list of args, like so:
args = {'a', 1, 'b', 2};
% Then expand this into arguments:
output = func(args{:})
Run Code Online (Sandbox Code Playgroud)
Jas*_*n S 11
这里有一堆非常明显的功能,不时有用:
mfilename (返回当前运行的MATLAB脚本的名称)dbstack (允许您访问matlab函数堆栈的名称和行号)keyboard (停止执行并将控制权交给调试提示符;这就是调试提示符中有K的原因 K>>dbstop error (自动将您置于调试模式,在触发错误的行停止)Azi*_*zim 10
使用nargin为可选参数设置默认值,并使用nargout设置可选输出参数.快速举例
function hLine=myplot(x,y,plotColor,markerType)
% set defaults for optional paramters
if nargin<4, markerType='none'; end
if nargin<3, plotColor='k'; end
hL = plot(x,y,'linetype','-', ...
'color',plotColor, ...
'marker',markerType, ...
'markerFaceColor',plotColor,'markerEdgeColor',plotColor);
% return handle of plot object if required
if nargout>0, hLine = hL; end
Run Code Online (Sandbox Code Playgroud)
gno*_*ice 10
我喜欢使用函数句柄有很多原因.首先,它们是我在MATLAB中发现的最接近指针的东西,因此您可以为对象创建类似引用的行为.你可以用它们做一些简洁(和简单)的事情.例如,替换switch语句:
switch number,
case 1,
outargs = fcn1(inargs);
case 2,
outargs = fcn2(inargs);
...
end
%
%can be turned into
%
fcnArray = {@fcn1, @fcn2, ...};
outargs = fcnArray{number}(inargs);
Run Code Online (Sandbox Code Playgroud)
我只是认为像这样的小事很酷.
用于操作数组的冒号运算符.
@ ScottieT812,提到一个:展平数组,但是选择数组的所有其他变体:
x=rand(10,10);
flattened=x(:);
Acolumn=x(:,10);
Arow=x(10,:);
y=rand(100);
firstSix=y(1:6);
lastSix=y(end-5:end);
alternate=y(1:2:end);
Run Code Online (Sandbox Code Playgroud)
哦,并反转一个阵列
v = 1:10;
v_reverse = v(length(v):-1:1);
Run Code Online (Sandbox Code Playgroud)
赋值左侧的条件参数:
t = (0:0.005:10)';
x = sin(2*pi*t);
x(x>0.5 & t<5) = 0.5;
% This limits all values of x to a maximum of 0.5, where t<5
plot(t,x);
Run Code Online (Sandbox Code Playgroud)
了解您的轴属性!您可以设置各种各样的东西来调整默认绘图属性以执行您想要的操作:
set(gca,'fontsize',8,'linestyleorder','-','linewidth',0.3,'xtick',1:2:9);
Run Code Online (Sandbox Code Playgroud)
(例如,将fontsize设置为8pt,所有新行的线条样式都是实线,宽度为0.3pt,xtick指向[1 3 5 7 9])
在使用聚合函数时要严格指定维度,例如min,max,mean,diff,sum,any,all,...
比如这条线:
reldiff = diff(a) ./ a(1:end-1)
Run Code Online (Sandbox Code Playgroud)
可能很好地计算向量中元素的相对差异,但是如果向量退化为仅一个元素,则计算失败:
>> a=rand(1,7);
>> diff(a) ./ a(1:end-1)
ans =
-0.5822 -0.9935 224.2015 0.2708 -0.3328 0.0458
>> a=1;
>> diff(a) ./ a(1:end-1)
??? Error using ==> rdivide
Matrix dimensions must agree.
Run Code Online (Sandbox Code Playgroud)
如果为函数指定了正确的尺寸,则该行返回一个空的1×0矩阵,这是正确的:
>> diff(a, [], 2) ./ a(1, 1:end-1)
ans =
Empty matrix: 1-by-0
>>
Run Code Online (Sandbox Code Playgroud)
对于通常计算矩阵上的列的最小值的最小函数也是如此,直到矩阵仅由一行组成. - 然后它将返回行的最小值,除非维度参数另有说明,并且可能会破坏您的应用程序.
我几乎可以向您保证,因此设置这些聚合函数的尺寸将为您节省大量的调试工作.
至少对我来说就是这种情况.:)
为了能够快速测试一个函数,我这样使用nargin:
function result = multiply(a, b)
if nargin == 0 %no inputs provided, run using defaults for a and b
clc;
disp('RUNNING IN TEST MODE')
a = 1;
b = 2;
end
result = a*b;
Run Code Online (Sandbox Code Playgroud)
稍后,我添加了一个单元测试脚本来测试不同输入条件的函数.
| 归档时间: |
|
| 查看次数: |
31295 次 |
| 最近记录: |