combine data into hourly-based data
显示 更早的评论
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
采纳的回答
更多回答(1 个)
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!