Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Matlab Matrices transferred pass-by-value or pass-by-reference?

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?

like image 508
Yunus Eren Güzel Avatar asked Feb 25 '12 12:02

Yunus Eren Güzel


1 Answers

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.

like image 182
Oli Avatar answered Sep 20 '22 11:09

Oli