I have a data set as below , How can i assign T values : 49 as 1 and 53 as 5 and so on considering the gap between the time (49,53,54...10,16) in seconds, it is always not 4 s at the begining and end , it vary as 2 s , 3 s, and so on... i would be

T(s) Cnt
49.00 804.00
53.00 717.00
54.00 613.00
55.00 603.00
56.00 630.00
57.00 580.00
58.00 566.00
59.00 643.00
1.00 672.00
2.00 651.00
3.00 675.00
4.00 650.00
5.00 653.00
6.00 605.00
7.00 567.00
8.00 678.00
9.00 760.00
10.00 824.00
16.00 798.00

 Risposta accettata

Assuming your data is held in a matrix as follows:
data = [49.00 804.00
53.00 717.00
54.00 613.00
55.00 603.00
56.00 630.00
57.00 580.00
58.00 566.00
59.00 643.00
1.00 672.00
2.00 651.00
3.00 675.00
4.00 650.00
5.00 653.00
6.00 605.00
7.00 567.00
8.00 678.00
9.00 760.00
10.00 824.00
16.00 798.00];
The difficulty comes from your time wrapping around back to 0 after 60 seconds. You can cope with that by calculating the time difference between rows modulo 60:
timeoffsets = mod(diff(data(:, 1)), 60);
To get back to your time but starting at 1, you can then cumsum these offsets:
data(:, 1) = [0; cumsum(timeoffsets)] + 1

5 Commenti

Thanks Guillaume for the answer. but I am in the middle of my programme,
data=[s t];
data = data(data(:,1)>=10,:);
data = data(data(:,2)~=0,:);
T = unique(data(:,2));
S = arrayfun(@(x)(sum(data(data(:,2)==x))), T);
Data=[T,S];
timeoffsets = mod(diff(Data(:, 1)), 60);
Data(:, 1) = [0; cumsum(timeoffsets)]+1;
it is now giving me the answer like this:
T(s) cnt
1.00 672.00
2.00 651.00
3.00 675.00
4.00 650.00
5.00 653.00
6.00 605.00
7.00 567.00
8.00 678.00
9.00 760.00
10.00 824.00
16.00 798.00
49.00 804.00
53.00 717.00
54.00 613.00
55.00 603.00
56.00 630.00
57.00 580.00
58.00 566.00
59.00 643.00
could you please give some advice:
note: my original data set has more than 1 value related each time, first I sum all the counts related to each time value, then I apply your script.
So, isn't the answer you get what you wanted? If not, what did you want? I'm unclear on what you're asking now.
Note: you could use accumarray instead of arrayfun, but that won't change the output of your program one bit:
[T, ~, subs] = unique(data(:, 2));
S = accumarray(subs, data(:, 2));
See attached file, I have a data set like that, 6th column display the time, hh-mm-ss.xxx format. so i wanted to sum GS data (column 2-5) for every second. So I have wrote a code like this,
data=[s t];
data = data(data(:,1)>=10,:);
data = data(data(:,2)~=0,:);
T = unique(data(:,2));
S = arrayfun(@(x)(sum(data(data(:,2)==x))), T);
fprintf('%.2f %.2f\n', [T S].');
Then I have the answer like this
T (s) cnt
49.00 804.00
53.00 717.00
54.00 613.00
55.00 603.00
56.00 630.00
57.00 580.00
58.00 566.00
59.00 643.00
1.00 672.00
2.00 651.00
3.00 675.00
4.00 650.00
5.00 653.00
6.00 605.00
7.00 567.00
8.00 678.00
9.00 760.00
10.00 824.00
16.00 798.00
but, I want to convert time values starting with 1. that mean T=49 should be 1 , but I can not get the answer I want... hope you can get an idea now. can you give me some advices
Your algorithm will break if the time span of a file is greater than a minute since you may get identical seconds that belong to different minutes. unique will consider the same. While there are ways to cope with this if you assume identical seconds are consecutive, it seems to me that the easiest thing is to simply not discard the hour and minute information.
I don't know how you read your file but the following would work:
data = readtable('Data.txt', 'ReadVariableNames', false); %or use textscan
hms = regexp(data.Var6, '(\d+)-(\d+)-(\d+)', 'tokens', 'once');
hms = str2double(vertcat(hms{:}));
[hms, ~, rows] = unique(hms, 'rows');
summeddata = zeros(size(hms, 1), 4);
for column = 2:5
summeddata(:, column-1) = accumarray(rows, data{:, column});
end

Accedi per commentare.

Più risposte (0)

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by