passing by reference an object in a function
18 次查看(过去 30 天)
显示 更早的评论
I have a simple question about passing a variable by reference in Matlab. from the forums I have seen, there is no clear answer about that. From what I have understood( but may be I am wrong), let say that you have a function that take a table A and want to change in this table only the h line. if I do
function arg=myFunction(A,h)
A(h,:) = A(h,:)+2;
end
and call myFunction(A,3), A will not be changed. When specifying myFunction like this
function A=myFunction(A,h)
A(h,:) = A(h,:)+2;
end
and calling myFunction(A,3), A is changed. But from what I understood of what matlab does, matlab is copying A, then change in the copy the h lines of A, then provide output the copy. First question: Is this true or not? and if yes, is there a way to pass it by reference, which means directly modifying A. I know that by doing directly A(h,:) = A(h,:)+2; in the command line, I can obtain the same result. but on some applications, when A is very large, like Gibbs sampler, where i want to sample sequentially each line of A conditionnally to the other ones, it looks to me that copying for every call the large dataset A is inefficient. I am not an expert of matlab, please excuse if the question is stupid.
1 个评论
Adam
2015-6-9
In general in Matlab everything is copied by value, yes. The link James gives below should be sufficient for your needs I think.
The other approach which is what I always use is to use Object-oriented programming.
Classes that are derived from the handle class are always passed by reference.
采纳的回答
James Tursa
2015-6-9
编辑:James Tursa
2015-6-11
For inplace operations on data using functions, see this Blog by Loren:
Basically, there are rules you must follow for the function signature, and you must call the function from within another function.
Regarding your question about what MATLAB normally does for an arbitrary function argument, the basic answer is this:
If the function is a mex routine, MATLAB passes the address of the original variable (i.e., not a shared data copy). The mex routine is expected to treat all input arguments as read-only.
If the function is a m-file and the variable is not an OOP object derived from handle, MATLAB passes a shared data copy of the variable (i.e., a new variable but no data copy yet). If the function subsequently changes the passed variable, the variable is first unshared (i.e., a data copy takes place) and then the changes are made to this newly unshared variable.
If the function is a m-file and the variable is an OOP object derived from handle, MATLAB basically allows the function to make changes to the variable inplace (i.e., data is allowed to be altered inplace with no data copy).
Note that struct and cell array references (e.g., mystruct.field1 or mycell{3}) always create a temporary shared data copy of the field or cell element. So if you have this expression as an agrument a temporary shared data copy will always be passed, even to a mex routine.;
Loren's Blog talks about how to alter some of this behavior to get MATLAB to operate inplace in some circumstances.
2 个评论
Adam
2015-6-10
编辑:James Tursa
2015-6-11
I'm not sure about java objects to be honest as I very rarely use them. All other Matlab objects are passed by value, not be reference. In terms of memory allocation for taking copies though it isn't quite that simple. If the object is not actually altered within the function it is passed to then no new memory is allocated. Only if the object is edited does new memory get allocated for the hard copy.
But yes, if you derive a class from handle then every object of that class will be passed by reference.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!