Reputation: 1209
I have some code that currently reads:
data = repmat(1:10, 1, 2);
N = 6;
period = 10;
result = NaN * zeros(1, period);
for i=1:period
range_indices = i:i+N;
temp_data = data(range_indices);
result(i) = sum( temp_data .* fliplr(temp_data));
end
I'm trying to make this faster (for larger datasets, e.g. period = 2000 and N = 1600), but I'm unable to get this into a form where it's a matrix operation (e.g. by using conv or xcorr).
Upvotes: 1
Views: 40
Reputation: 4768
You should be able to completely linearise this. Firstly, consider the range_indices
. These have the form:
1 -> N
2 -> N+1
...
P -> N+P
where P is the period. We can set up a matrix of these values like so:
range_indices = bsxfun(@plus,1:N,(1:period)'-1);
We can use these to grab the data directly, like so:
temp_data = data(range_indices);
It is then fairly simply to complete the function:
result = sum(temp_data.*fliplr(temp_data));
Finally, this isn't really related to the question, but just something I thought I'd point out - in future if you need to generate a matrix of NaN values, you should use nan(1,period)
instead.
Upvotes: 1