Are Matlab Matrices passed by value or by reference?

I am new to Matlab. You may find this question silly, but I really wonder if the following instruction is a field in order or a step-by-step procedure.

I = imread('logo.png'); binaryImage = im2bw(I, 0.4); Itemp = binaryImage; 

Is Itemp new matrix whose values ​​are copied from binaryImage , or is it just a pointer to binaryImage ?

+7
source share
2 answers

It is passed by reference until you change Itemp .

When changing Itemp matlab will copy binaryImage to Itemp and then change it.

I did some interesting tests, and then I went. If you do:

 A=rand(100);B=A;C=B;D=A;E=B; 

only one copy is stored in memory. If you change A

 A(1)=1; 

Then matlab will make one a new copy of the matrix for new A , and the variables B , C , D , E still point to the matrix of old A

+12
source

Matlab uses copy-on-write strategy

+12
source

All Articles