Effacer les filtres
Effacer les filtres

alter a matrix

1 vue (au cours des 30 derniers jours)
Richard
Richard le 1 Fév 2012
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?

Réponse acceptée

Luke
Luke le 1 Fév 2012
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 commentaire
Richard
Richard le 2 Fév 2012
that works great thanks. I find it amazing how I'm constantly learning new commands in matlab (such as histc).
thanks again.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by