필터 지우기
필터 지우기

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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by