alter a matrix

3 次查看(过去 30 天)
Richard
Richard 2012-2-1
I have a matrix ('data') which is composed of day of year in the first column and temperature data in the second column and also a vector ('dDates') of values which also represent day of year. e.g.
clear all
n = 366;
day = linspace(1+1/24,n,(n-1)*24)';
temp = rand(8760,1);
data = [day,temp];
dDates = [12, 32, 45, 67];
I'm trying to alter 'data' so that it only contains data which is measured for the 120 rows following the day number specified in 'dDates' although being the same size as the original 'data' i.e. the other rows filled with nans.
Each measurement in 'data' refers to one hour so 120 refers to 5 days worth of data.
So far I have used:
[r,c,v] = find(data(:,1)>dDates(1),1,'first');
inside a loop to find the row number of each element of 'dDates' in 'dates' but am finding it difficult to bring everything together to produce the outcome that I need. How should I go about doing this?

采纳的回答

Luke
Luke 2012-2-1
Try this:
[~,bin]=histc(dDates,day);
idx=false(size(data,1),1);
for k=1:length(bin)
idx(bin(k)+1:bin(k)+120)=1;
end
result=NaN(size(data));
result(idx,:)=data(idx,:);
I took "... data ... following the day number..." to mean that you don't want the row that matches dDates in the result. If you do, change bin+1:bin+120 to bin:bin+119 in the above.
  1 个评论
Richard
Richard 2012-2-2
that works great thanks. I find it amazing how I'm constantly learning new commands in matlab (such as histc).
thanks again.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by