9

I'm new at Matlab. You may find this question silly but I really wonder if the statement below is a pass-by-value operation or pass-by-reference operation.

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

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

Yunus Eren Güzel
  • 3,018
  • 11
  • 36
  • 63
  • 2
    Why are you worried about the internals of Matlab? From the programmer's point of view it is a copy of the array. Internally it is probably a reference counted object that will make a copy if you modify it. – QuentinUK Feb 25 '12 at 12:59

2 Answers2

13

It's pass by reference, until you modify Itemp.

When you modify Itemp matlab will copy binaryImage to Itemp and then modify it.

I made some interesting tests a while a go. If you do:

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

only one copy is kept in memory. If you modify A

A(1)=1;

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

Oli
  • 15,935
  • 7
  • 50
  • 66
13

Matlab uses a copy-on-write strategy

CitizenInsane
  • 4,755
  • 1
  • 25
  • 56