matlab - find first and last value for unique julian date -


i have data set similar following:

bthd = sort(floor(1+(10-1).*rand(10,1))); bthd2 = sort(floor(1+(10-1).*rand(10,1))); bthd3 = sort(floor(1+(10-1).*rand(10,1)));  depth = [bthd;bthd2;bthd3]; jday = [repmat(733774,10,1);repmat(733775,10,1);repmat(733776,10,1)];  temp = 10+(30-10).*rand(30,1);  data = [jday,depth,temp]; 

where have matrix similar 'data' julian date in first column, depth in second, , temperature in third column. find first , last values each unique jday. can obtained by:

data = [jday,depth,temp];  [~,~,b] = unique(data(:,1),'rows');  j = 1:length(unique(b));     top_temp(j) = temp(find(b == j,1,'first'));     bottom_temp(j) = temp(find(b == j,1,'last')); end 

however, data set extremely large , using loop results in long running time. can suggest vectorized solution this?

use diff:

% example jday = [1 1 1 2 2 3 3 3 5 5 6 7 7 7]; last = find( [diff(jday) 1] ); first = [1 last(1:end-1)+1]; top_temp = temp(first) ; bottom_temp = temp(last); 

note solution assumes jday sorted. if not case, may sort jday prior suggested procedure.


Comments