Alternative to permute-reshape

5 views (last 30 days)
Peter Ahlgren
Peter Ahlgren on 24 Feb 2014
Dear all
I would like to optimize my code with regards to speed in the following setting:
I have a data matrix where each row represents data for a given date (let us say daily data) and each column is a particular variable. The simulation results in a 3-dimensional array, where dim 1 are rows in the simulation, dim 2 represents individual variables and dim 3 represents time in the simulation. It is not possible for me to change the order of dimensions. An example could be that I have 10 variables with a history of 500 dates (the data variable will be of size 500x10) and the result would be 50000x10x50 meaning that I simulate 50000 paths for 10 variables 50 days ahead.
Right now the code looks like
indx = SimulateDateIndices(data,...);
out = permute(reshape(data(indx,:),size(indx,1),size(indx,2),size(data,2)),[1 3 2]);
where indx in the above example is a 50000x50 matrix that for each of the 50000 paths contains information about which dates in the data that follows each other.
It seems to me that this is not the most optimal way of doing things (1/3 of the time spent with simulating is used in this step that is rather simple compared to what else is going on) but I have a lack of ideas on how to optimze the code.
I hope some of you might have some ideas. All the best and thanks a lot in advance
Peter

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!