Using Interp1 when data has duplicates

19 次查看(过去 30 天)
Hi,
I am working with Timeseries data. Time vs Population in a building. I have data from two different sources for the space population. The plot is shown in the image attached. I need to interpolate these two plot lines to identify Y(population) values from both sources at regular intervals of X(Time) values (i.e. Population at 1-min increment in time). I tried interp1 but both my data sources have duplicates (as my population changed multiple times within a min). The time steps for both data sources dont match in the original data. I need to keep the duplicates as they are required data points. I am not sure how to work around this issue to obtain Y values from both sources at the same time-steps.
PS: I used clear 'x' and 'y' values and then reobtain x and y values but it only gives me the same data points that I used to plot.
Thanks for the help.
  4 个评论
Kevin Phung
Kevin Phung 2019-2-4
ok, but just to clarify, you want one line to describe both of those plots correct?
Krishna Chaitanya Simma
编辑:Krishna Chaitanya Simma 2019-2-4
I added a picture for what I am trying to do. From the two plot lines, I want to divide the time (x-axis) into multiple equal steps and interporlate the Y values (both y1 and y2). And then, since time becomes constant for Y values from both sources, I want to plot them against each other as shown in the diagram below in the image posted. (ex: y1 vs y2).
Did I explain it clearly?

请先登录,再进行评论。

采纳的回答

Krishna Chaitanya Simma
The following code worked. Incase anyone has similar issues. Instead of intrerp1, I used griddedInterpolant.
M = csvread('rdata24.csv'); % Read the data file for wifi routers
x1 = M(:,1);
y1 = M(:,2);
N = csvread('cdata24.csv'); % Read the data file for people counters
x2 = N(:,1);
y2 = N(:,2);
figure(1)
plot(x1,y1,x2,y2)
[x2,al,idx] = unique(x2,'stable'); % identify the non duplicate values
y2 = y2(al);
hold on
F = griddedInterpolant(x2,y2);
G = griddedInterpolant(x1,y1);
x1q = linspace(0,1,300); % Generate points to interpolate
y1q = G(x1q);
y2q = F(x1q);
hold on
plot(x1q, y1q, '*')
plot(x1q, y2q,'ro')
hold off
figure(2)
scatter(y1q,y2q);
  1 个评论
Tehreem Syed
Tehreem Syed 2022-8-11
Hey! I tried your methods but it's not working.
Attached you can see the snippet of my code.
Thank you :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by