Replacing Arrays and Matrices
2 ビュー (過去 30 日間)
Is it a good idea to overwrite an array with matrix? For example -
Or over-writing matrix/array with a singular value
Or both steps one after the other, in succession?
I tried to find any information related to this, but I was unable to find such. Though, there's a good chance my search query might not have optimal.
回答 (2 件)
Stephen23 2022 年 9 月 7 日
編集済み: Stephen23 2022 年 9 月 7 日
"Is it a good idea to overwrite an array with matrix?"
There is no difference: MATLAB does not have a "matrix" class: all numeric arrays are numeric arrays, regardless of their size (scalar, vector, matrix, ND). Ditto for logical, char, cell, etc arrays. They are all stored as a linear list in memory, then only thing that changes is the header information giving the array size (this is also why RESHAPE is very efficient).
The MATLAB documentation does recommend "Create new variables if data type changes — Create a new variable rather than assigning data of a different type to an existing variable. Changing the class or array shape of an existing variable takes extra time to process."
But I do not see anything in the documentation recommending against reusing variable names in general. I doubt that reassigning variable names will be a significant bottle-neck in your code.
Bruno Luong 2022 年 9 月 7 日
編集済み: Bruno Luong 2022 年 9 月 7 日
IMO nothing hurts of using same variable name for different things.
But my experience tell me that is a bad habit of programming. Someone, including you sometime later, who wants to modify the code might get confuse with the homonymous naming.
The good thing in favor of such pratice is that the memory footprint is reduced because you clear out the( old) variable of the worspace.