Replacing Arrays and Matrices

Is it a good idea to overwrite an array with matrix? For example -
y=(1:20);
y=y'*y;
Or over-writing matrix/array with a singular value
y=nnz(y>20);
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.

Antworten (2)

Stephen23
Stephen23 am 7 Sep. 2022
Bearbeitet: Stephen23 am 7 Sep. 2022

1 Stimme

"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.

5 Kommentare

Dyuman Joshi
Dyuman Joshi am 7 Sep. 2022
Thanks for the response and the link particularly, Stephen.
I was wondering how would it affect memory-wise? The example I gave was a small one in-terms of memory. Consider, y as 1x1e6 double vector. Would there be any difference to it?
Stephen23
Stephen23 am 7 Sep. 2022
"I was wondering how would it affect memory-wise?"
In what sense? You assign one array to a variable name, then reassign that name to another array. If you use the "old" array in the RHS then for a few moments both arrays will be stored in memory until the "old" one can be garbage collected. But if you used different variable names then you would have both in memory anyway.
Bruno Luong
Bruno Luong am 7 Sep. 2022
It matters AFTER the reassignment.
My bad, I should have clarified this earlier. Case at hand -
y=(1:1e4)';
y=y*y';
z=nnz(y>1e4);
%assigning to a new variable
y=nnz(y>1e4);
%overwriting
Does overwriting take time compared to assigning it to a new variable? I think it might depends on the size of array being overwritten?
Bruno Luong
Bruno Luong am 7 Sep. 2022
Bearbeitet: Bruno Luong am 7 Sep. 2022
"Does overwriting take time compared to assigning it to a new variable? I think it might depends on the size of array being overwritten?"
No. The extra time is for MATLAB to clear the memory of the old variable, which is negligible and does not depend on the sides of either variable.
I already tells you in my answer below "nothing hurts" accepted the programming habit and memory footprint.

Melden Sie sich an, um zu kommentieren.

Bruno Luong
Bruno Luong am 7 Sep. 2022
Bearbeitet: Bruno Luong am 7 Sep. 2022

1 Stimme

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.

1 Kommentar

Dyuman Joshi
Dyuman Joshi am 7 Sep. 2022
Bearbeitet: Dyuman Joshi am 7 Sep. 2022
That's my first time hearing or rather reading the word - Homonymous!
I appreciate your response!

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 7 Sep. 2022

Bearbeitet:

am 7 Sep. 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by