Does Matlab create implicit ranges when selecting contiguous portions of a matrix?

4 views (last 30 days)
(Using Matlab R2015a on Win7x64)
I'm implementing a time-domain autocorrelation function which takes an input X and creates two "views" into the array, then does an element-by-element multiply.
% x is 360000-by-3 matrix
for lag = 0:whatever
x1 = x(1+lag:end,:);
x2 = x(1:end-lag,:);
out(lag,:) = mean(x1 .* x2) ./ v; % where v is precomputed var(x) which is 1x3 vector
end
The profiler shows a lot of time is spent in assigning x1 and x2. I'm wondering the following:
1. Does Matlab create a hidden intermediate array (1+lag:end, etc) as indexing into the array? Or do indices specified with colon receive special optimized treatment compared to a standalone array with the same syntax? eg is
x1 = x(1:100)
different from
rng=1:100;
x1 = x(rng);
I think the colon syntax used as indices is special since you can use 'end' here but not outside the parentheses. So I don't think an intermediate array is being created.
2. Does Matlab create a copy of the data? I thought Matlab did copy-on-write, so no need to copy array, which leads me to believe the time is spent creating indices 1:length(x). Then again, (next question)...
3. If this is the case, then does Matlab somehow internally refer to "views" or "slices" of the array like Numpy, wherein the view is really just a data structure with start, stop, stride, byte size, etc? I'm guessing the answer is No, since if it were the case there would be insignificant time spent assigning x1 and x2. So my conclusion is that Matlab is not creating an intermediate array, but is still copying the data to x1 and x2, rather than just moving some pointers around.
4. Is there any way to speed this up? Something with circshift, diff, etc? I'm using a similar process to compute Allan variance, where I'm taking shifted sections of the data without actually modifying it.
(5. BTW if you know why this yields drastically different results from the frequency-domain version of autocorrelation, please let me know! :) )
Thanks

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!