combine data into hourly-based data
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
ahmad Saad
am 21 Aug. 2023
Kommentiert: ahmad Saad
am 21 Aug. 2023
Col2 is a time column and i need to classify the attached data to be hourly-based data.
for example:
for 0 < col2 <=1 get the median of corresponding values of col3 (and col4)
for 1 < col2 <=2 get the median of corresponding values of col3 (and col4)
.
.
.
.
for 23 < col2 <=24 get the median of corresponding col3 (and col4)
so, i get a matrix of three columns:
c4= [i median(col3) median(col4)];
where i =1:24
my trial:
for i=1:24
id=find(data(:,2)>i-1 & data(:,2)>i)
m1(i)= median(data(id,3));
m2(i)= median(data(id,4));
c4(i,[1:3])=[i m1(i) m2(i)];
end
Any help
0 Kommentare
Akzeptierte Antwort
Voss
am 21 Aug. 2023
load data.mat
First, your approach, modified:
for i=1:24
id=find(data(:,2)>=i-1 & data(:,2)<i);
m1(i)= median(data(id,3));
m2(i)= median(data(id,4));
c4(i,[1:3])=[i m1(i) m2(i)];
end
disp(c4)
Another approach to calculate the medians for each hour:
hr = discretize(data(:,2),0:24);
[g,g_id] = findgroups(hr);
meds = splitapply(@(x)median(x,1),data(:,[3 4]),g);
disp(meds);
Then, if you don't want the final result to include medians for hours where there is no data:
c4 = [g_id meds];
disp(c4);
Or if you do want to include those NaN medians:
c4 = NaN(24,3);
c4(:,1) = 1:24;
c4(g_id,[2 3]) = meds;
disp(c4);
0 Kommentare
Weitere Antworten (1)
Dyuman Joshi
am 21 Aug. 2023
There's no need of using find in the for loop
load('data.mat')
for i=1:24
%Comparison was incorrect
id=data(:,2)>=i-1 & data(:,2)<i;
m1(i)= median(data(id,3));
m2(i)= median(data(id,4));
c4(i,[1:3])=[i m1(i) m2(i)];
end
disp(c4)
%Now the MATLAB/vectorized approach
%Data
vec=data(:,2);
%Specify the bins
bins = 0:24;
%Discretize into bins with inclusion of the right side
%as described in the problem statement i.e. loweredge < data <= upperedge
idx=discretize(vec,0:24,'IncludedEdge','right');
%Accumulate according to the indices obtained by discretization
%and apply median function to the data
%Specify the output size as a column vector as indices are a column vector as well
%And the number of sets will be 1 less than the number of bins
fun = @(x) accumarray(idx,data(:,x),[numel(bins)-1 1],@median);
%Desired output
out = [(1:24)' fun(3) fun(4)];
disp(out)
The only difference is that the for loop approach yields NaN, where as accumarray approach gives 0, for no values in a particular bin.
Siehe auch
Kategorien
Mehr zu Biological and Health Sciences finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!