Having problems with loops
古いコメントを表示
Hi, I have a file with a 29661x2 array. From that file the first column consists of numbers from 1 to 29661. The second column, consists of timestamps. What I want is to every 60 seconds calculate the rate using the formula of (number in column 1) divided by (time in column 2). for example the beginning of my data looks like this:
if true
% code
end
1 1.0388
2 1.222
3 2.691833
4 2.862933
5 7.049433
6 10.501633
7 11.089067
8 11.122933
9 12.9572
10 13.499233
11 13.9066
12 16.985033
13 21.246567
14 21.5275
15 27.307267
16 28.023233
17 32.218567
18 35.5397
19 35.773767
20 45.421833
21 50.658233
22 50.8893
23 51.586667
24 52.077567
25 52.506367
26 52.899867
27 53.974267
28 54.6747
29 54.772033
30 56.044233
31 56.292667
32 56.361633
33 57.394533
34 59.001533
35 59.837167
36 61.208533
So what I want is to the code to go through the second column, look for the closest number to 60 (in this portion would be 59.837167) and calculate the rate (35/59.837167 for example). Then check the closest timestamp to 120 and do the same and continue calculating it every 60 seconds (more or less). Because my timestamps are not integers I can't just make a loop calculating the rate every 60 seconds exactly. Instead I thought of a loop that would check if the value of the timestamp is between 59 and 60 and then calculate the rate. Continuing with then checking next if it is between 119 and 120, and so on. I made this code, but for some reason I can;t figure out it is stopping after after calculating the rate between 359 and 360 seconds. Any idea what Im missing?
if true
clc, clear all, close all
d = importdata('eightfirsthourv5.txt');
a = 60;
b = 59;
i=1;
array = zeros(30000,2);
for i = 1:1:29661
if d(i,2)<a && d(i,2)>b
a = a+60;
b = b+60;
X = d(i,1)/d(i,2);
array(i,1) = i;
array(i,2) = X;
end
end
My variables after running it end up a= 360 array 3000x2 double b = 359 d = 29661x2double i = 29661 X = 1.4031
回答 (2 件)
Star Strider
2016 年 6 月 21 日
I would use interp1 instead, and do something like this:
Data = [1 1.0388
2 1.222
3 2.691833
4 2.862933
5 7.049433
6 10.501633
7 11.089067
8 11.122933
9 12.9572
10 13.499233
11 13.9066
12 16.985033
13 21.246567
14 21.5275
15 27.307267
16 28.023233
17 32.218567
18 35.5397
19 35.773767
20 45.421833
21 50.658233
22 50.8893
23 51.586667
24 52.077567
25 52.506367
26 52.899867
27 53.974267
28 54.6747
29 54.772033
30 56.044233
31 56.292667
32 56.361633
33 57.394533
34 59.001533
35 59.837167
36 61.208533];
ti = [Data(1,2):60:Data(end,2)]'; % Time Interpolation Vector
pos = interp1(Data(:,2), Data(:,1), ti);
Velocity = pos ./ ti
Velocity =
962.6492e-003
587.7611e-003
You will have to experiment with this to get the result you want with your code, but it eliminates the problem of having to search through your data with a for loop.
Roger Stafford
2016 年 6 月 22 日
編集済み: Roger Stafford
2016 年 6 月 22 日
You can use a second-order approximation for your derivative as follows. If x are the numbers in the first column and t the time values in the second column, then for three values, (x1,t1), (x2,t2), and (x3,t3), a formula for the approximate second order derivative at t2 is:
dx/dt = (x2-x1)/(t2-t1)*(t3-t2)/(t3-t1) + (x3-x2)/(t3-t2)*(t2-t1)/(t3-t1)
Note that this is a weighted average between the two quantities (x2-x1)/(t2-t1) and (x3-x2)/(t3-t2). This formula has the property that if x is a quadratic function of t, the approximation is actually exact.
If your original array is called A, here is how the code would go:
n = size(A,1);
x = A(:,1);
t = A(:,2);
xe = [x(3);x;x(n-2)]; % Special treatment of the two endpoints
te = [t(3),t,t(n-2)];
d21 = (xe(2:n+1)-xe(1:n))./(te(2:n+1)-te(1:n));
d32 = (xe(3:n+2)-xe(2:n+1))./(te(3:n+2)-te(2:n+1));
dxdt = (d21.*(te(3:n+2)-te(2:n+1))+d32.*(te(2:n+1)-te(1:n)))./(te(3:n+2)-te(1:n));
The vector dxdt is then a column vector giving the approximate derivative at each corresponding point in A.
I realize I haven't followed your direction to obtain the derivative every 60 seconds, but hopefully the above might give you some ideas. For example, using 'interp1' on these derivative values with cubic interpolation.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!