【问题标题】:find first and last value for unique julian date查找唯一朱利安日期的第一个和最后一个值
【发布时间】:2013-03-21 20:59:41
【问题描述】:

我有一个类似于以下的数据集:

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];

我有一个类似于“数据”的矩阵,第一列是 Julian Date,第二列是深度,第三列是温度。我想找出每个唯一 Jday 的第一个和最后一个值是什么。这可以通过以下方式获得:

Data = [Jday,Depth,temp];

[~,~,b] = unique(Data(:,1),'rows');

for j = 1:length(unique(b));
    top_temp(j) = temp(find(b == j,1,'first'));
    bottom_temp(j) = temp(find(b == j,1,'last'));
end

但是,我的数据集非常大,使用此循环会导致运行时间很长。谁能建议一个矢量化的解决方案来做到这一点?

【问题讨论】:

    标签: matlab


    【解决方案1】:

    使用diff:

    % for 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);
    

    请注意,此解决方案假定 Jday 已排序。如果不是这种情况,您可以在建议的程序之前sortJday

    【讨论】:

      【解决方案2】:

      您应该能够使用 unique 函数的出现选项来完成此操作:

      [~, topidx, ~] = unique(Data(:, 1), 'first', 'legacy');
      [~, bottomidx, ~] = unique(Data(:, 1), 'last', 'legacy');
      
      top_temp = temp(topidx);
      bottom_temp = temp(bottomidx);
      

      如果您使用的是 MATLAB R2013a,则需要使用 legacy 选项。如果您运行的是 R2012b 或更早版本,您应该可以将其删除。

      【讨论】:

      • 两次使用unique是不是有点多余?特别是如果Jday 已经排序...
      • @Shai 好点。您的解决方案绝对是更好的解决方案,但我觉得值得注意的是 unique 可以完全按照要求完成。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多