Clear Filters
Clear Filters

combine data according to values in another vector in matlab

1 view (last 30 days)
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.

Accepted Answer

Bob Thompson
Bob Thompson on 25 Feb 2019
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 Comment
Helene
Helene on 16 Mar 2019
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!