博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Matlab函数参数传引用方法
阅读量:5809 次
发布时间:2019-06-18

本文共 1957 字,大约阅读时间需要 6 分钟。

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  () and  () 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  () on Loren Shure's blog.

 

from: 

转载于:https://www.cnblogs.com/nn0p/archive/2013/01/16/2862159.html

你可能感兴趣的文章
考研太苦逼没坚持下来!看苑老师视频有点上头
查看>>
HCNA——RIP的路由汇总
查看>>
zabbix监控php状态(四)
查看>>
定时任务的创建
查看>>
实战Django:小型CMS Part2
查看>>
原创]windows server 2012 AD架构试验系列 – 16更改DC计算机名
查看>>
统治世界的十大算法
查看>>
linux svn安装和配置
查看>>
SSH中调用另一action的方法(chain,redirect)
查看>>
数据库基础
查看>>
表格排序
查看>>
updatepanel中的GridView中的radiobuttonList怎么设置样式
查看>>
关于Android四大组件的学习总结
查看>>
java只能的round,ceil,floor方法的使用
查看>>
由于无法创建应用程序域,因此未能执行请求。错误: 0x80070002 系统找不到指定的文件...
查看>>
新开的博客,为自己祝贺一下
查看>>
puppet任务计划
查看>>
【CQOI2011】放棋子
查看>>
采用JXL包进行EXCEL数据写入操作
查看>>
***CodeIgniter框架集成支付宝即时到账支付SDK
查看>>