简体   繁体   中英

Matlab equivalent for Python's id function

Does Matlab have any equivalent to Python's id function ? I tried some Python-like syntax to create a copy of a Matlab table and was surprised that it actually created a table with a variable whose value was the table, and whose variable name was the table variable identifier.

I'm trying to get a sense of how assignment works in Matlab, whether it is by reference or creates a copy, and I'd like to get the address of the object if at all possible.

The closest to Python's id that I know in Matlab is the undocumented format debug . With this format option, typing a variable name in the command window shows information about its memory usage.

The displayed pr field is a pointer to the real part of the data, and pi is for the imaginary part. (Credit to @rayryeng for this information .) Starting from Matlab R2018a, the pi pointer no longer exists, because of the new interleaved complex storage.

For example, the following illustrates Matlab's copy-on-write mechanism. Note how the statement y = x does not create a copy of x (it has the same pr as y has), but modifying an entry of y later on triggers the copy ( pr changes).

>> format debug
>> x = 1:5;
>> y = x;
>> x, y
x =
Structure address = 37751590
m = 1
n = 5
pr = ff4bbe80
pi = 0
     1     2     3     4     5
y =
Structure address = 37751590
m = 1
n = 5
pr = ff4bbe80
pi = 0
     1     2     3     4     5

>> y(5) = 10;
>> x, y
x =    
Structure address = 37751590
m = 1
n = 5
pr = ff4bbe80
pi = 0
     1     2     3     4     5
y =    
Structure address = 37751de0
m = 1
n = 5
pr = 85c71e0
pi = 0
     1     2     3     4    10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM