是否可以在Matlab中获得对矩阵的引用? [英] Is it possible to get a reference to a matrix in Matlab?

查看:84
本文介绍了是否可以在Matlab中获得对矩阵的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A具有一个结构,该结构具有四个非常大的矩阵,分别对应于灰度图像.根据函数的输入,我想将这四个矩阵之一存储在变量中.但是,由于这些矩阵很大,因此我不想创建它们的副本.

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.

Matlab中是否有与C ++中的引用概念类似的东西?换句话说:是否有两个变量指向同一矩阵?

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使用.这意味着,如果将数组(或所有数组)传递给函数,则除非您写入数组,否则它们不会重复.换句话说,您可能不需要做想做的事.

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.

例如,如果您将图像存储在具有firstImagefourthImage字段的结构imgStruct中,则传递例如imgStruct.fourthImage作为函数的输入,即使被调用,数组也不会重复. inputImage在函数内部.

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).

这篇关于是否可以在Matlab中获得对矩阵的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆