How reshape and variable assignments are handled internally within MATLAB?

4 次查看(过去 30 天)
Does anyone know what exactly happens when you are trying to reshape a variable? So, let's say I have a large array called largeArray (of course) and I am reshaping it as follow:
largeArray=reshape(largeArray,s1,s2,s3,...,sn);
So, I am resizing largeArray and assigning it to itself. Is MATLAB recreating a whole new variable, also called largeArray, therefore, the data is actually copied? or does the MATLAB just goes internally change the dimension, without recopying the entire data?
The following code takes a long time:
n=8*1024*1024*1024/8;
largeArray=zeros(n,1);
largeArray=reshape(largeArray,64,n/64); %this line takes a long time, even longer than the above command.
%Actually so long that I force quit MATLAB. Didn't wait for it.
however, this doesn't take a long time:
n=8*1024*1024*1024/8;
largeArray=zeros(n,1);
largeArray=reshape(largeArray,32,n/32); %this returns almost immediately.
also assigning seems to behave funny:
n=8*1024*1024*1024/8;
largeArray=zeros(n,1);
newLargeArray=largeArray; % this comes back almost immediately
newLargeArray(1)=42; % This again takes long but similar to that of the zeros(n,1), i.e. when creating the array;

采纳的回答

the cyclist
the cyclist 2015-10-9
I think at least some of your questions are answered in this documentation page about memory allocation.
For example, in your last example,
newLargeArray=largeArray;
does not require another copy of that array, because MATLAB can just reference the first one (since they are identical). But as soon as you do
newLargeArray(1)=42;
the arrays are no longer identical, so MATLAB has to make an actual copy.
Regarding the two reshape commands ... They both return very fast for me, as expected. MATLAB does not need to makes copies. (Even though they are different "shapes", they are stored in the same way, but with different indexing.)
  2 个评论
Mohammad Abouali
Mohammad Abouali 2015-10-9
Thank you the cyclist.
I still don't get it why
largeArray=reshape(largeArray,32,n/32)
and
largeArray=reshape(largeArray,64,n/64)
take considerably different time. Actually this behavior prompted my question.
the cyclist
the cyclist 2015-10-9
I don't know why. These are effectively identical times for me in R2015b on latest OS X.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2015-10-9
MATLAB generates a new descriptor with the same information as the other descriptor except for the dimensions, with both of them pointing to the same block of memory. The memory itself is not touched. The operation of creating a descriptor is done all the time in MATLAB, for the results of every operation, so it should be very fast.
  1 个评论
Mohammad Abouali
Mohammad Abouali 2015-10-9
编辑:Mohammad Abouali 2015-10-9
Thank you Walter.
So, when I do
mean(reshape(largeArray,5,[]))
in order to calculate the mean in blocks of 5 elements; a new descriptor is used and passed, but the memory is not copied then. Correct?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by