# Correlation values of various time periods

Asked by Richard on 13 Mar 2012
Latest activity Answered by Geoff on 13 Mar 2012

The code below is a simplification to a problem that I am working through:

```clear all
Data.L1 = 1 + (20-1)*rand(8760,1);
Data.L2 = 1 + (20-1)*rand(8760,1);
Data.L3 = 1 + (20-1)*rand(8760,1);
Data.L4 = 1 + (20-1)*rand(8760,1);
```
```DateTime=datestr(datenum('2011-01-01 00:00','yyyy-mm-dd HH:MM'):1/24:...
datenum('2011-12-31 23:00','yyyy-mm-dd HH:MM'),...
'yyyy-mm-dd HH:MM');
DateTime=cellstr(DateTime);
Here, I have a time vector ('DateTime') where each row corresponds to a row in Data.L1,2,3, and 4. Therefore gives the time of year when each measurement in Data was taken.
```

My task is to find the correlation between each pair of locations in Data. This is simple enough by using choosek to find all of the possible combination and then using corrcoef to find the the correlation. However, my next task is to find the relevance of measuring data at hourly intervals when looking at the corrrelation. Therefore in order to do this I'm trying to produce a plot of different correlation values measured through time (thus having time on the xaxis and correlation on the y axis).

In order to complete this task I need to find the correlation when data is measured at hourly intervals (as described above) and then do the same process but for data measured at 2 hour intervals, thus take one measurement every two rows of 'Data' which would reduce the size of the vectors in Data by half. I can do this fine manually, but I'm trying to extend this to look at every possible measurement frequencies i.e. meausred every 3 hours, 4 hours, 5 hours... and so on up to 90 days (which refers to one measurment per season).

As I said before, this can be done manually but would take a long time and would involve a rather sizeable script. Therefore I am looking for advice on the best way of doing this.

I hope I've been clear in describing what I'm trying to do.

## Products

No products are associated with this question.

Answer by Geoff on 13 Mar 2012

It appears you just want an index that slices the data in intervals:

```N = length(Data.L1);
```
```idx1hr = 1:N;
idx2hr = 1:2:N;
idx3hr = 1:3:N;
...
idx90d = 1:90*24:N;
```

If you really want everything in between (is there actually any point in having 90 days minus 1 hour as the interval?) you can programmatically generate these ranges into a cell array. Normally you'd have the interval size increase exponentially at some point.

```intervals = cell;
for step = 1:90*24
cell{step} = 1:step:N;
end
```

Of course, this only gives you the intervals starting at 1. If you want to generate the entire range for every step size, you're going to have an N/step-by-N sized matrix for each range. You can use ndgrid or meshgrid cleverly to generate such matrices.

-g-