1

次のようなデータセットがあります。

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

ここで、最初の列にユリウス日、2 番目の列に深さ、3 番目の列に温度を持つ「データ」に似たマトリックスがあります。一意の 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

ただし、私のデータ セットは非常に大きく、このループを使用すると実行時間が長くなります。これを行うためのベクトル化されたソリューションを提案できる人はいますか?

4

2 に答える 2

2

unique関数の発生オプションを使用してこれを達成できるはずです。

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

top_temp = temp(topidx);
bottom_temp = temp(bottomidx);

MATLAB R2013a を使用している場合は、レガシー オプションが必要です。R2012b 以前を実行している場合は、削除できるはずです。

于 2013-03-21T10:35:05.407 に答える
2

使用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、ソートされていると想定していることに注意してください。そうでない場合はsort Jday、提案された手順の前に行うことができます。

于 2013-03-21T10:17:41.977 に答える