• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Matlab函数参数传引用方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Can MATLAB pass by reference?

If you are attempting to use pass-by-reference to modify the input argument passed into a function, the answer to the question depends on whether the input is a handle object or a value object. The two types of objects are described in the Object-Oriented Programming (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brfylq3.html) and Programming Fundamentals (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/brtm8si.html) sections of the documentation. By default, objects (including matrices, arrays, etc. of the built-in data types) are value objects.

Handle objects do exhibit reference behavior when passed as function arguments; value objects do not. When you pass a handle object to a function, MATLAB still copies the value of the argument to the parameter variable in the function (with one bit of subtlety; see below.) However, all copies of a handle object refer to the same underlying object.

If a function modifies a handle object passed as an input argument, the modification affects the object referenced by both the original and copied handles. In this case, the function does not need to return the result to be reassigned.

If instead you are attempting to use pass-by-reference to avoid unnecessary copying of data into the workspace of the function you\'re calling, you should be aware that MATLAB uses a system commonly called "copy-on-write" to avoid making a copy of the input argument inside the function workspace until or unless you modify the input argument. If you do not modify the input argument, MATLAB will avoid making a copy. For instance, in this code:

function y = functionOfLargeMatrix(x)
y = x(1);

MATLAB will not make a copy of the input in the workspace of functionOfLargeMatrix, as x is not being changed in that function. If on the other hand, you called this function:

function y = functionOfLargeMatrix2(x)
x(2) = 2;
y = x(1);

then x is being modified inside the workspace of functionOfLargeMatrix2, and so a copy must be made.

For more information on this behavior, read this posting (http://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/) on Loren Shure\'s blog.

 

from: http://matlab.wikia.com/wiki/FAQ#Can_MATLAB_pass_by_reference.3F


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Delphi]用Delphi7访问.NET 2.0的WebService发布时间:2022-07-18
下一篇:
Delphi中的字符串函数(5)-SysUtils中的Ansi字符串函数发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap