# alter a matrix

3 ビュー (過去 30 日間)
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 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 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.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by