フィルターのクリア

How reshape and variable assignments are handled internally within MATLAB?

4 ビュー (過去 30 日間)
Mohammad Abouali
Mohammad Abouali 2015 年 10 月 8 日
コメント済み: the cyclist 2015 年 10 月 9 日
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by