Matlab矩阵是按值传递还是按引用传递? [英] Are Matlab Matrices transferred pass-by-value or pass-by-reference?

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

问题描述

我是Matlab的新手.您可能会发现这个问题很愚蠢,但我真的很想知道下面的语句是按值传递操作还是按引用传递操作.

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;

Itemp是一个新的矩阵,其值是从binaryImage复制的,还是只是一个指向binaryImage的指针?

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

推荐答案

它已通过引用传递,直到您修改Itemp.

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

修改Itemp时,matlab会将binaryImage复制到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;

内存中仅保留一份副本.如果您修改A

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

A(1)=1;

然后,matlab为新的A生成矩阵的一个新副本,并且变量BCDE仍指向矩阵旧的A.

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.

这篇关于Matlab矩阵是按值传递还是按引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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