Passing Matrix using call by reference
29 次查看(过去 30 天)
显示 更早的评论
When looking for call-by-reference in matlab I found the Handle-Class. What I don't yet understand is how I use that class on a matrix so that I can pass a reference of the matrix and not the matrix itself.
I know matlab already kind of does this with large matrices, but firstly, I don't know how big my matrices will become, and secondly, I want to make sure it is only with reference, since it is done very often and is a performance critical operation.
Thank you.
0 个评论
采纳的回答
Vaclav Rimal
2013-12-10
To really take the advantage of handle class, you would need to create your own class based on the class handle. Create a file called largematrix.m having the following code:
classdef largematrix < handle
properties
array
end
methods
function myfunc(obj)
obj.array=obj.array+1;
end
end
end
Then, you can create an object of that class. The variable representing the object in the workspace is in fact a pointer to the part of memory which stores the object. Running the method myfunc behaves like you probably want. The following commands
a=largematrix;
a.array=ones(4);
a.myfunc; % or myfunc(a), which has the same result
a.array
produce
ans =
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
6 个评论
Vaclav Rimal
2013-12-11
Daniel, the methods will not be copied, for they are the same for all objects you create (assuming they are the same class). The methods should behave just like ordinary functions. I think this is not what matters. Matlab certainly deals this efficiently.
An issue can arise when you create and delete many instances. This depends on what exactly you want to do and whether you can aviod recreating objects by e.g. overwriting their properties by new values (which wouldn't repeatedly capture and release memory).
更多回答(4 个)
Jos (10584)
2013-12-10
Yes, using assignin and input name will do that for you
function myfun(varargin)
assignin('caller',inputname(1),2)
>> test = 'hallo'
test =
hallo
>> myfun(test)
>> test
test =
2
Sean de Wolski
2013-12-10
编辑:Sean de Wolski
2013-12-10
What operation are you doing inside of myfunc? Is it elementwise?
If so, MATLAB will do the operation in place if x is named the same everywhere:
x = magic(10);
x = myfunc(x);
%%
function x = myfunc(x)
x = x.^2;
end
Since the operation is being done in-place, no memory copy will be necessary.
Jos (10584)
2013-12-10
Matlab does not create a copy, until it is really needed. So passing big arrays to functions uses no extra memory, except when you change the big matrix inside the function.
function myfun1(X)
Y = X ; No copy is made, Y is a pointer (a reference to X)
X(1) = 1 % this creates a copy of X inside the workspace of the function. Note that Y is untouched.
Y(1) = 1 ; % this now also creates a copy
2 个评论
sixwwwwww
2013-12-10
Define you function as below:
function y = myFunc(x)
y = 2;
end
then try this in command window:
x = 1;
x = myFunc(x);
It will return you value 2 which you need
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Genetic Algorithm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!