alter a matrix

조회 수: 4 (최근 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에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by