Calculating time differences

Hello, I have data which look like this:
t = [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...19 20 21 22 23 24 25...29 30 31 32 33 34 35 36 37 38 39 40...]
x = [ 5 5 5 5 5 5 5 5 5 4 3 3 4 4...4 3 2 2 3 4 5...5 4 4 4 4 5 5 5 5 5 5 4...]
where t is time in seconds and x represents values for each second. There is over 1000 time points and corresponding values in my data. Every 10s, values start to decrease and then go up again. I'd like to calculate the time difference between each the 10s points (10s, 20s, 30s etc.) and the time where the values starts to increase and save it(the time difference) in a vector form. The interesting points in the example above are in bold for better understanding of the problem. For given data the vector will be v=[3 3 4 ...]
Thanks in advance

 채택된 답변

Jan
Jan 2012년 4월 11일

1 개 추천

Let's start with a loop:
x = [5 5 5 5 5 5 5 5 5 4 3 3 4 4 4 4 4 4 4 3 ...
2 2 3 4 5 5 5 5 5 4 4 4 4 5 5 5 5 5 5 4];
x = reshape(x, 10, []);
n = size(x, 2);
v = zeros(1, n);
vi = 0;
for i = 1:n
p = find(diff(x(:, i)) > 0, 1);
if ~isempty(p)
vi = vi + 1;
v(vi) = p + 1;
end
end
v = v(1:vi);
Does this solve the problem correctly? Is the speed acceptable?

댓글 수: 1

Marcel
Marcel 2012년 4월 11일
It solves the problem indeed. Thanks very much for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2012년 4월 11일

0 개 추천

p = find(diff(x) > 0);
v = rem(p, 10);
Perhaps some fine-tuning is required. E.g. I assume you need to add 1 to the results.
[EDITED] Or:
y = reshape(x, 10, []);
[row, col] = find(diff(x) > 0, 1); % WRONG!
v = rem(col, 10);
Or:
p = find(diff(x) > 0);
p(rem(p, 10) == 0) = []; % Remove multiples of 10
v = rem(p, 10);
Again: I cannot test this currently.

댓글 수: 4

Marcel
Marcel 2012년 4월 11일
Thanks for the answer... it's close to what I'm looking for but it finds all differences(for increasing values) and I need only the first ones after every 10s period, if you know what I mean. Any idea how to remove the rest of it from the v vector?
Jan
Jan 2012년 4월 11일
See EDITED.
Marcel
Marcel 2012년 4월 11일
hmm... the first edit does give only one value (first after 10s) and the second one does the same thing like the original one (displays all differences after each 10s period).
If I take first 40s based on the example I gave above I should get vector v= [3 3 4] and using first 'edit' I get only v=[3] and for the second 'edit' v = [3 3 4 5 4]. P.S. I have added 1 to each of the results produced by your codes.
Jan
Jan 2012년 4월 11일
I'll take a look on the problem in the later evening, when I have access to Matlab - if it is not solved already.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

질문:

2012년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by