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创建一个矩阵的新副本code>,
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屋!