A have a struct with four very large matrices that correspond to grayscale images. Depending on the input of my function, I want to store one of those four matrices in a variable. However, since those matrices are very large, I don't want to create a copy of them.
Is there something similar in Matlab to the concept of reference in C++? Or in other words: is it possible to have two variables pointing to the same matrix?
Matlab uses a "lazy copy on write" for variables. That means that if you pass your array (or all of them) to your function, they won't be duplicated unless you write into the array(s). In other words, you may not need to do what you want to do.
For example, if you store your images in a structure imgStruct
with fields firstImage
to fourthImage
, and you pass e.g. imgStruct.fourthImage
as input to the function, the array is not duplicated, even if it is called e.g. inputImage
inside the function.
If you plan to write to the image, you can create a handle class to store your image data, which is passed by reference (thus, if you modify the image inside the function, it is also modified in your base workspace).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With