Filter löschen
Filter löschen

combine data according to values in another vector in matlab

1 Ansicht (letzte 30 Tage)
Helene
Helene am 25 Feb. 2019
Kommentiert: Helene am 16 Mär. 2019
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.

Akzeptierte Antwort

Bob Thompson
Bob Thompson am 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 Kommentar
Helene
Helene am 16 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by