Why is reshape so fast? (Spoiler: Copy-on-Write)

Viewed 4601

I have a big matrix A which is 1GB of double values, when I reshape it to different dimensions, it's incredible fast.

A=rand(128,1024,1024);
tic;B=reshape(A,1024,128,1024);toc

Elapsed time is 0.000011 seconds.

How can it be that fast? Another observation, MATLAB uses less memory than it should after running that code and storing two matrices of 1GB each: Memory used by MATLAB: 1878 MB (1.969e+09 bytes)

1 Answers
Related