Equalising the number of points in a graph
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Charles Mitchell-Thurston
el 6 de Jun. de 2022
So i have two graphs, which both have 96 points along x that make them up (e.g. ygraph1 = 1x96 double, xgraph1 = 1x96 double). I am trying to write something like:
sum = 0
for i = 1:length(x)
diffsquared = (ygraph1(i,1)-ygraph2(i,1))^2
sum = sum + diffsquared
end
The problem is that the x values of the graphs dont line up.
They should be this shape/scale width wise, by that i mean that the two peaks on each graph should be this far apart from each other, i cannot stretch the blue one otherwise that would mean the y values are not lined up correctly.
Hopefully you can see my problem, is there anyway around this? (if you cant see what i mean then i am happy to explain more)
2 comentarios
Geoff Hayes
el 6 de Jun. de 2022
@Charles Mitchell-Thurston - are you sure that the 96 x points for each graph are identical? Can you attach the data for each?
Respuesta aceptada
Torsten
el 6 de Jun. de 2022
Editada: Torsten
el 6 de Jun. de 2022
X = sort(union(xgraph1,xgraph2));
ygraph1 = interp1(xgraph1,ygraph1,X,'linear','extrap');
ygraph2 = interp1(xgraph2,ygraph2,X,'linear','extrap');
Then do the operations on the arrays as needed with (X,ygraph1) and (X,ygraph2).
2 comentarios
Más respuestas (1)
Walter Roberson
el 6 de Jun. de 2022
mn1 = min(x1) ;
mn2 = min(x2) ;
mn = max(mn1, mn2);
mx1 = max(x1) ;
mx2 = max(x2) ;
mx = min(mx1, mx2) ;
mask1 = x1 >= mn & x1 <= mx;
mask2 = x2 >= mn & x2 <= mx;
xss1 = x1(mask1) ;
yss1 = y1(mask1) ;
xss2 = x2(mask1) ;
yss2 = y2(mask2) ;
ny1 = interp(xss1, yss1, xss2) ;
ny2 = interp1(xss2, yss2, xss1) ;
ds1 = sum((yss2-ny1).^2);
ds2 = sum((yss1-ny2).^2);
diffsquared = ds1 + ds2;
Note that the results are the sum of squared errors over a different number of points that before. The data is first restricted to the common range, and then each variable is predicted at the points in the other x subset.
If the set of x was exactly the same between the two lines, then the result would be to count each squared error twice. You should consider using mean squared error -- divide ds by the sum of the length of ds1 and ds2
1 comentario
Ver también
Categorías
Más información sobre Live Scripts and Functions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!