Matlab矩阵是按值传递还是按引用传递? [英] Are Matlab Matrices transferred pass-by-value or pass-by-reference?
问题描述
我是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
生成矩阵的一个新副本,并且变量B
,C
,D
,E
仍指向矩阵旧的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屋!