0

I have several large matrices that should be passed to different functions many times in an iterative algorithm. How can I avoid the unnecessary copy of variables to the function to speed up the program? Is there any way to group these matrices in a structure or class and pass them by reference or pointer?

a=zeros(1000,1000);
b=zeros(1000,1000);
d=myfunction(a,b);
a=myfunction2(b,d);
....

Thanks.

Update: I should have provided more details as I see in the comments. I have several large matrices. Few of them remain constant in the program and others change in each function at each iteration. So if I pass these matrices by value, Matlab makes a copy of that, changes the value and again copy them as the output of the function and then they go out of scope and get destroyed and at each iteration all these unnecessary copying make the program awfully slow. If I were to program it in C++, I would use object oriented programming for these matrices and I would pass them as reference to the function but I don't know if that's possible in MATLAB.

Eman
  • 165
  • 1
  • 1
  • 8
  • 2
    If `myfunction` does not modify `a` and `b`, then there will be no copy of variables, they will be passed as reference. MATLAB is pretty smart and does all these things under the hood – Ander Biguri Sep 02 '16 at 15:35
  • See http://stackoverflow.com/questions/38113315/matlab-variable-passing-and-lazy-assignment/ – Sam Roberts Sep 02 '16 at 16:25
  • @AnderBiguri They're not passed by reference, they're passed by value with copy-on-write behaviour. See my answer to http://stackoverflow.com/questions/38113315/matlab-variable-passing-and-lazy-assignment/ – Sam Roberts Sep 02 '16 at 16:28
  • @SamRoberts yes, you are rigth! The duplicate should fix that! – Ander Biguri Sep 02 '16 at 16:54

1 Answers1

1

Ander's comment is absolutely true. That said, if you do need the functions to modify the variable contents, then you might want to think about refactoring the code to try to avoid this scenario. I know the code that you posted is just a simplified example, but you could look at that and ask questions like "do a and b need to be initialized outside of the function? Could the function initialize them and return them instead?".

If you really can't get around it, then you could encapsulate the data in a handle class and pass the handle class to the functions instead.

CKT
  • 781
  • 5
  • 6
  • Thing is, if OP can't get really around it, it means that he/she will need, even if this was C, make a copy. There is, in general, no need for anything fancy – Ander Biguri Sep 02 '16 at 16:03
  • I don't see how it necessarily means there has to be a copy made, ever. In the example, he's initializing two matrices to zeros, then presumably the function is doing something to them. It doesn't appear, at least from the example, that he has pre-existing data he has to avoid writing over. The end goal here seems to be to generate a solution without it getting copied a bunch of times along the way. But I don't know. If that's the case, then a handle class as a container would do the trick. – CKT Sep 02 '16 at 16:06
  • @CKT Thanks for the answer. Some of the matrices remain constant throughout the program and other matrices are changed in all functions and the newly obtained matrices are used in the next function and so on. There is no other way around as the program is quite large. I could use global variables which make it hard to debug and also is a bad programming style or I could put these matrices in a few structures or classes and pass them as references. – Eman Sep 02 '16 at 17:11