フィルターのクリア

combine data according to values in another vector in matlab

1 回表示 (過去 30 日間)
Helene
Helene 2019 年 2 月 25 日
コメント済み: Helene 2019 年 3 月 16 日
I have a vector t that records time points between 0 and 10 (not evenly spaced) and a vector vals that records values of some outputs at the corresponding time points. So vals is of same length as t.
t = [0 0.02 0.18 0.21 0.4 0.4 ... 9.03];
vals = [1.1 0 5.9 2.7 6 1.2... 15.3];
The underline process is periodic of period 2 and I would like to look at the outputs in one period. So I calculated tp to get the distribution of those time points in one period.
tp = sort(unique(mod(t,2)/2));
Next step is to calculate a vector vals_pd of accumulated values in one period. For example in the vector vals, both 6 and 1.2 correspond to time point 0.4 in the vector t. 0.4 corresponds to 0.2 in tp. Then in the vector vals_pd, I want 6+1.2 be at the same position as 0.2 in tp.
How can I obtain the vector vals_p? Any help would be appreciated.

採用された回答

Bob Thompson
Bob Thompson 2019 年 2 月 25 日
I know others can probably do better than I can at this, but here goes.
data = [t; vals];
tims = uniques(t);
for i = 1:length(tims)
data = [data(:, data(1,:) <tims(i)),[tims(i);sum(data(2,data(1,:) == tims(i)))],data(:,data(1,:)>tims(i))];
end
data = data';
data = sortrows(unique(mod(t,2)/2));
  1 件のコメント
Helene
Helene 2019 年 3 月 16 日
Thank you Bob! The loop does the job. I use another matrix to store the data as I am always a little hesitate to modify a matrix inside a loop.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by