combine data according to values in another vector in matlab

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by