Subset a time-series using matlab - matlab

I have a one minute interval time series from which I want to subset 3 columns of data.
The time format is dd/mm/yy hh:mm:ss
I want to specify a 20 min time value for which I want to extract the corresponding samples for all the corresponding days (19:00 ; 19:20 ; 19:40 ; 20:00).
I already created a time series using
ts = timeseries(data, time)
samples=getdatasamples(ts, i)
But I am having trouble defining the logical vector i that can do such extraction

Please try this code:
pat_19='19:[0 2 4]0:00';
pat_20='20:00:00';
out_19=~(cellfun('isempty',regexpi(a(:,1),pat_19,'match')));
out_20=~(cellfun('isempty',regexpi(a(:,1),pat_20,'match')));
out=a(find(out_19+out_20),:);
Here, I assumed that the value of seconds is always '0'
Please see the example below:
"a" is a cell array with date as first column and data values of time series.
a =
'15/08/81 19:00:00' 0.01
'15/08/81 19:10:00' 0.02
'15/08/81 19:20:00' 0.03
'15/08/81 19:30:00' 0.04
'15/08/81 19:40:00' 0.06
'15/08/81 19:50:00' 0.07
'15/08/81 20:00:00' 0.01
'15/08/81 20:10:00' 0.02
'15/08/81 20:20:00' 0.03
'15/08/81 20:30:00' 0.03
after executing the above code, the output is stored in the cell array "out"
out =
'15/08/81 19:00:00' 0.01
'15/08/81 19:20:00' 0.03
'15/08/81 19:40:00' 0.06
'15/08/81 20:00:00' 0.01

Related

Simulink misses data points in a from-workspace block for discrete simulation

I have a simulation running at 50 Hz, and some data that comes in at 10 Hz. I have extra 'in-between' points with dummy data at the following 50 Hz time points, and interpolation set to off. This should in theory ensure that between 10 Hz time steps, the dummy data is being held and only at the 10 Hz steps is the actual data present. For example, my data vector would be
[0.0 0.8 0.1 0.12 0.2 0.22 0.3 0.32 0.4 0.42 0.5 0.52 ...
-1 -1 1 -1 2 -1 3 -1 4 -1 5 -1 ...]
However, with a scope attached directly from the 'from-workspace' block, simulink is returning this:
[0.0 0.8 0.1 0.12 0.2 0.22 0.3 0.32 0.34 0.4 0.42 0.5 0.52...
-1 -1 -1 -1 2 -1 3 3 -1 4 -1 5 5...]
where some values are skipped and others are repeated in a consistent pattern. Is there something with simulinks time-step algorithms that would cause this?
Edit: A solution I ended up finding was to offset the entire time vector by 1/100th of a second so that the sim was taking data between points rather than on points, and that seemed to fix it. Not ideal, but functional.

Matrix columns correlations, excluding self-correlation, Matlab

I have a couple of matrices (1800 x 27) that represent subjects and their recordings (3 minutes equivalent for each of 27 subjects). Each column represents a subject.
I need to do intercorrelation between subjects, let's say to correlate F to G, G to H, and H to F for all 27 subjects.
I use CORR command corr(B) where B is a matrix and it returns the next example:
1 0.07 -0.05 0.10 0.04 0.12
0.07 1 -0.02 -0.08 0.17 0.03
-0.05 -0.02 1 0.04 0.16 0.13
0.10 -0.08 0.04 1 -0.04 0.34
0.04 0.18 0.16 -0.04 1 0.13
How can I adjust the code to exclude self-correlation (eg F to F) so I won't get "1" numerals?
(it's present in each row/column)
I have to perform some transformations afterwards, like Fisher Z-Transformation, which returns inf for each "1", and as result, I can't use further calculations.

How to select the average value from 3 matrices

I am new to MATLAB and I need help. I have 3 matrices (A, B, and C) and I want to create a new matrix average_ABC that contains average values.
A = [ 0.3 0.5 0.9
0.14 0.36 0.1
0.9 0.5 0.14]
B = [ 0.8 0.9 0.14
0.1 0.25 0.4
0.8 0.14 0.25]
C = [0.25 0.3 0.47
0.12 0.3 0.2
0.14 0.56 0.9]
The resulting matrix will be
average_matrix = [ 0.3 0.5 0.47
0.12 0.25 0.2
0.8 0.5 0.25]
Please, any suggestion, how can I do it?
You can first concatenate your matrices along the third dimension (using cat) and then compute whatever you want using the dim parameter that is available for most functions to specify that you want to perform that operation along the third dimension.
Also you've stated that you want the average (mean), but based on your example you actually want the median. Either way, we can compute them using this method.
data = cat(3, A, B, C);
% Compute the mean
mean(data, 3)
% 0.45 0.56667 0.50333
% 0.12 0.30333 0.23333
% 0.61333 0.4 0.43
% Compute the median (which seems to be what you actually want)
median(data, 3)
% 0.3 0.5 0.47
% 0.12 0.3 0.2
% 0.8 0.5 0.25
I hope this will work
average_matrix=(A+B+C)/3.;

Is it possible to filter a matrix by a tagged array?

I have a 10 dimensional matrix with many, many columns (in the hundreds of thousands). I have however, implemented a tag based on the day of an experiment and a condition
So my original matrix looks something like
0.1 0.25 0.64 0.15 0.1 0.96 0.01 0.05....
.
.
.
.
0.2 0.3 0.049 0 0.3 0.71 0.4 0.45....
I was able to implement a tag for the day and experiment type so my matrix looks like
0.1 0.25 0.64 0.15 0.1 0.96 0.01 0.05....
.
.
.
.
0.2 0.3 0.049 0 0.3 0.71 0.4 0.45....
1 1 1 1 2 2 2 2
1 1 2 2 2 3 3 3
The top row represents a day, and the bottom row represents a condition. Is there anyway to "filter" this matrix, call it A, by day and condition in MATLAB? So for example, if I want the day 1 condition 2 "mini-matrix", I can get
0.64 0.15
.
.
.
0.049 0
Yes, you can do this by accessing only the columns matching a certain value in your day or condition rows.
For example, say your input matrix is A, and that the entries in the third row A(3,:) are the days, and the entries in the fourth row A(4,:) are the conditions.
Then A(:, A(3,:) == 2) will give you the subset of columns in A where the day is 2.
And A(:, A(3,:) == 2 & A(4,:) == 1) will give you the columns where the day is 2 and the condition is 1.

reading only the daily julian values of a data given for every 3 hr

The data I am using is available per 3 hour time step. The Julian date for the data is therefore in an increasing order of 3/24 = 0.125 for each row of data value. I am interested only on the daily time step data and I would like to get help how to read only the daily Julian values that are recorded after every 8 Excel rows using Matlab.
Example of my data:
0.125
0.25
0.375
0.5
0.625
0.75
0.875
1
1.125
1.25
1.375
1.5
1.625
1.75
1.875
2
2.125
2.25
2.375
2.5
2.625
2.75
2.875
3
3.125
3.25
3.375
3.5
3.625
3.75
3.875
4
.
.
[continues until 360 and starts back from 0.125]