For loop to evaluate Root Mean Square

For every row of C I want to find all the elements in column A that are in a time window (centered on the first element of column A at the first iteration) of 4 minutes. Second iteration is find all the elements in column A that are in a time window (centered on the second element of column A) of 4 minutes. And so on for all rows of C table.
After doing that, I want to delete all outputs that are equal mantaining one for type and compute root mean square on every output remaining.
Attached a dummy table for the code, I think this is relatively easy to do with a for loop but I don't know how to implement it, other solution are well accepted,
Thanks in advance

6 Comments

Here is a start of using for loop
s = 0;
k = 0;
for i = 1:n
if v(i) > 0
s = s + v(i)^2;
k = k + 1;
end
end
result = sqrt(s/k);
My problem isn't calculating root mean square there is the function
y = rms (x)
for that, my problem is calculating a function on a unknokwn number of measurements given a logical condition
You lack a clear description of your variables. The mat file you share only contains a single column of datetime values. Where are your values? Where are you start_time and end_time arrays?
It looks like you can fairly easily do this with an array operation, but without a better description of your data it is hard to help you.
Rik you're right I uploaded the wrong file, I'll try to make it easier to understand.
I want the code to repeat the function isbetween for every element of first column of C to determinate the values that are in the range of 2 minutes for every element. I expect to have a number of outputs that is equal to the number of elements in C column. After this operation I'll proceed to the RMS calculation on output of the for loop
Tstart = C(1,1);
Tstop = C(6103,1);
for i=Tstart:Tstop
tf = isbetween(C.T(i),C.T(i)-2,C.T(i)+2);
F = tf*C(A);
S = tf*C(B);
D = tf*C(T);
G = table(D,F,S)
Z = nonzeros(G)
end
Your code has multiple issues, so it will not run with the data you provided. Either provide working code (and explain how it should be sped up/optimized/extended), or provide a full example where you describe the steps you want to take in words.
Do you want to select all values in a 4-minute window, essentially performing a sliding window operation? If so, why would you need Tstart and Tstop?
I know my code isn't working, i posted it only to explain what i need. I try to describe it in words.
For every row of C I want to find all the elements in column A that are in a time window (centered on the first element of column A at the first iteration) of 4 minutes. Second iteration is find all the elements in column A that are in a time window (centered on the second element of column A) of 4 minutes. And so on for all rows of C table.

Sign in to comment.

Answers (1)

I don't really understand what you mean by conditions, but maybe you can get what you want by thinking of a 2-step process where the first step is to pick out the rows you want and the second step is to get the rms. This code snippet illustrates it:
stopTime = datetime(2020,1,5,8,15,0); % set these according to your conditions, possibly in for loop
startTime = datetime(2020,1,5,7,45,0);
wantRows = (T>startTime) & (T<stopTime);
y = rms(x(wantRows));

8 Comments

I was doing exactly what you're proposing, but I have a problem with the for loop, can you help me? C is attached.
I want the code to repeat the function isbetween for every element of first column of C to determinate the values that are in the range of 2 minutes for every element. I expect to have a number of outputs that is equal to the number of elements in C column
Tstart = C(1,1);
Tstop = C(6103,1);
for i=Tstart:Tstop
tf = isbetween(C.T(i),C.T(i)-2,C.T(i)+2);
F = tf*C(A);
S = tf*C(B);
D = tf*C(T);
G = table(D,F,S)
Z = nonzeros(G)
end
Sorry, I really cannot tell what you are trying to do from looking at your code. For example, you make G as a new table each time through the loop but you don't save it or anything computed from it.
From your description, my guess is that you want something like this (but I'm not at all sure):
rmsA = zeros(height(C),1);
for i=1:height(C)
tf = isbetween(C.T(i),C.T(i)-2,C.T(i)+2);
xA = C.A(tf);
rmsA(i) = rms(xA);
end
For every row of C I want to find all the elements in column A that are in a time window (centered on the first element of column A at the first iteration) of 4 minutes. Second iteration is find all the elements in column A that are in a time window (centered on the second element of column A) of 4 minutes. And so on for all rows of C table.
After doing that, I want to delete all outputs that are equal mantaining one for type and compute root mean square on every output remaining.
@Jeff, looks like that's what he should mean, although you can't just subtract 2, because that would subtract 2 entire days.
rmsA = zeros(height(C),1);
for i=1:height(C)
tf = isbetween(C.T(i),C.T(i)-minutes(2),C.T(i)+minutes(2));
xA = C.A(tf);
rmsA(i) = rms(xA);
end
This code isn't doing what I need, the output is the same value on every row. I need instead a set of tables where I can recognize what values are used and what values are not used to compute rms. I'll make an example of my desired output:
At the end of loop I want a number of tables (1 column tables) equal to the number or rows of C (6103), and these table are made of zeros or ones according to the results of isbetween. I need to delete all tables that are equal (mantaininig one for each different output), and after this I'll compute the root mean square of values of A on every different table
But that is already contained in the logical array tf.
I don't think the code is doing what I need. If you try to execute it you'll have an output with only one value repeated for every row. But for how the data is build (3 different time arrays concatenated that are far more than 2 minutes each other) I expect at least 3 different values, but there is only one
I checked the code, there is an error in isbetween, the vector to analyze is C.T and not C.T(i). This is the right code.
Thank you all
rmsA = zeros(height(C),1);
for i=1:height(C)
tf = isbetween(C.T,C.T(i)-minutes(2),C.T(i)+minutes(2));
xA = C.A(tf);
rmsA(i) = rms(xA);
end

Sign in to comment.

Products

Release

R2019b

Asked:

on 9 Apr 2020

Commented:

on 11 Apr 2020

Community Treasure Hunt

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

Start Hunting!