MATLAB Answers

0

how to loop in one time to get the max value minus the min value.

Asked by Jan Risn on 11 Jul 2019
Latest activity Commented on by Jan Risn on 11 Jul 2019
I have data in the workspace in 2 columns
year/ month / date hour minute seconds data
(1856x2)cell
output1 =
2015 12 19 02 40 0 ​​ 0
2015 12 19 02 50 0 ​​ 0
2015 12 19 03 00 0 ​​ 0
2015 12 19 03 10 0​​ 2.2
2015 12 19 03 20 0 ​​ 0
2015 12 19 03 30 0 ​​ 4.6
2015 12 19 03 40 0 ​​ ​​ 4.6
2015 12 19 03 50 0 ​​ 4.6
2015 12 19 04 00 0 ​​ 4.6
2015 12 19 04 10 0 ​​ 4.6
2015 12 19 04 20 0 ​​ 4.6
2015 12 19 04 30 0 ​​ 4.6
2015 12 19 04 40 0 ​​ ​​ 4.6
2015 12 19 04 50 0 ​​ 4.6
2015 12 19 05 00 0 ​​ 4.6
2015 12 19 05 10 0 ​​ 4.6
2015 12 19 05 20 0 ​​ 4.6
2015 12 19 05 30 0 ​​ 4.6
2015 12 19 05 40 0 ​​ ​​ 4.6
2015 12 19 05 50 0 ​​ 3
......
I want to reduce data in 1 hour (50 minutes - 00 minutes)
the last minute data was reduced by the initial minute in one hour
example:
2015 12 19 03 50 0 4.6
-
2015 12 19 03 00 0 0
obtained value =
2015 12 19 03 00 0 4.6
So that i get 1 data in one hour.
if i get a value (-) minus is considered zero
and I get data in one hour on each date in one year.
I tried the following data script and I felt wrong in making the program.
T = output1(:,1);
D = datenum(T,'dd-mm-yy HH:MM');
Days = day(D);
Months = month(D);
Hours = hour(D);
Minutes = minute(D);
C = output1(:,2);
M = cell2mat(C);
max_minutes = zeros(23,2); min_days = zeros(23,2);
for i = 0:23
if nonzeros(M(Days==i))
[max_minutes(i,1) max_minutes(i,2)] = max(M(Days==i));
Hours_temp = Hours(Days==i);
Minutes_temp = Minutes(Days==i);
max_hour(i) = Hours_temp(max_minutes(i,2));
max_minute(i) = Minutes_temp(max_minutes(i,2));
[min_days(i,1) min_days(i,2)] = min(M(Days==i));
min_hour(i) = Hours_temp(min_days(i,2));
min_minute(i) = Minutes_temp(min_days(i,2));
else
max_minutes(i,1:2) = NaN;
min_days(i,1:2) = NaN;
end
end
I thank everyone who helped me in resolving my difficulties.
I use matlab 2014a, which uses the old function so there are some programs whose functions are not readable in my matrix

  0 Comments

Sign in to comment.

1 Answer

Answer by Andrei Bobrov
on 11 Jul 2019

[a,~,c] = unique(cdata{1}(:,1:4),'rows','stable');
out = [a,accumarray(c,cdata{2},[],@(x)max(x) - min(x))];

  6 Comments

load('sample.mat')
time1 = datevec(cdata(:,1));
[a,~,c] = unique(time1(:,1:4),'rows','stable');
out = [cellstr(datestr([a,zeros(size(a,1),2)])),...
num2cell(accumarray(c,cat(1,cdata{:,2}),[],@(x)max(x) - min(x)))];
last variant:
load('sample.mat')
time1 = datevec(cdata(:,1));
d = cat(1,cdata{:,2});
[a,~,c] = unique(time1(:,1:4),'rows','stable');
out = [cellstr(datestr([a,zeros(size(a,1),2)])),...
num2cell(accumarray(c,(1:numel(d))',[],@(x)innerfun(d,x)))];
function out = innerfun(d,x)
k = d(sort(x));
out = max(0,k(end) - k(1));
end
thank you very much sir, this is very helpful ..

Sign in to comment.