Passing Matrix using call by reference

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.

 Accepted Answer

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 Comments

Sounds interesting, especially with the methods I can place inside the class and extra data. Only question for me is, how efficient is it? If I define a lot of mehtods inside the new class and then create many objects of that kind, does everyone of the objects carry all the methods around? Performance is the issue for what I am programming. That started me on call-by reference in the first place.
Daniel, what are you trying to do?
I'm implementing a genetic scheduling algorithm that needs to be very fast. I admitt I do not know a lot about what is fast at the base level, but I am fairly certain that if I have many operations on the same set of data, that call-by-value costs just way more time than call-by-reference. Is that so or am I mistaken?
The follow up was around the fact, that since it is a genetic algorithm, I will have many instances of the object, getting created and deleted all the time. Will the methods I define in the class be copied every time I create a new instance? Do you know something about that?
You're right that a call by value will take more time, but I don't know that you'll be saving any time by overwriting the way you are since the memory copy still needs to happen.
Have you used the profiler to identify bottlenecks yet?
doc profile
Get the code working on a small scale first, then we can work on optimizing it.
True. Thanks for the tip.
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).

Sign in to comment.

More Answers (4)

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

1 Comment

Thanks for the idea, I'll definitely follow up that path. It's just not what I was looking for.

Sign in to comment.

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.

1 Comment

Operation isn't elementwise, but thanks for the info.

Sign in to comment.

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 Comments

I know this already. What I am looking for is a true call-by-reference ins this form:
x=1;
myFunc(x);
function myFunc(x)
x=2;
end
x
ans=2;
Is that possible?
Jos (10584)
Jos (10584) on 10 Dec 2013
Edited: Jos (10584) on 10 Dec 2013
OK. Yes, it is possible. See my second answer.

Sign in to comment.

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

1 Comment

Sorry, but please read the question. I don't want call by value, which is what Matlab usually does in your case. Exception to this is in the case of large matrices, where there is a mix of both. Again not what I need and not reliable.
I did research the issue before asking: http://www.mathworks.com/matlabcentral/answers/96960
Please look into the issue to see if the obvious is what was asked for before answering.

Sign in to comment.

Categories

Asked:

on 10 Dec 2013

Commented:

on 11 Dec 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!