马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?注册
x
本帖最后由 eecsseudl 于 2013-4-29 10:04 编辑
matlab程序效率低下,其中一个原因就是它的参数无法引用,每次都是传值。这不但 导致效率问题,要实现某些功能,也需要一些特殊的手段。比如最简单的,如果交换两个变量的值,也就是在C/C++里的函数void swap(int& a, int& b),在C/C++里实现很容易,但在matlab里,你会吗?
下面这个解决方法很巧妙,因为它的实现很有参考价值,附在下面欣赏一下。
functionswap(A,B)% SWAP - swap contents of two variables%
SWAP(A,B) puts the contents of variable A into variable B and vice versa.%
You canuseeitherfunctionsyntax 'swap(A,B)' or command syntax 'swap A B'.%%
Example:%
A = 1:4 ; B = 'Hello' ;%
swap(A,B) ;%
A % -> Hello%
B % -> 1
2
3
4%%
SWAP(A,B) is a convenient easy short-cutforother (series of)%