Equalising the number of points in a graph

1 visualización (últimos 30 días)
Charles Mitchell-Thurston
Charles Mitchell-Thurston el 6 de Jun. de 2022
Editada: Torsten 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
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?
Also, naming a local variable sum can lead to errors since sum is a built-in MATLAB function.
Charles Mitchell-Thurston
Charles Mitchell-Thurston el 6 de Jun. de 2022
@Geoff Hayes that is my problem, the 96 points are not the same in the x axis.
I have attatched my data aswell. In my data the data is called y and z axis. These are x and y respectively, they are called y and z for reasons that dont matter for the context of this question.

Iniciar sesión para comentar.

Respuesta aceptada

Torsten
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
Charles Mitchell-Thurston
Charles Mitchell-Thurston el 6 de Jun. de 2022
For interp1 i still have to input a method (interp1(x,v,xq,method,extrapolation)) would 'linear'be my best choice?
Charles Mitchell-Thurston
Charles Mitchell-Thurston el 6 de Jun. de 2022
Tried linear and the result is what i was looking for.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
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
Charles Mitchell-Thurston
Charles Mitchell-Thurston el 6 de Jun. de 2022
i get an error in
Error using interp
Expected r to be a scalar with value >= 1.
Error in interp (line 48)
validateattributes(r,{'numeric'},{'>=',1,'finite','integer','scalar'},'interp','r');
As all of the inputs of interp are arrays in the case you have written.
ny1 = interp(xss1, yss1, xss2) ;
ny2 = interp1(xss2, yss2, xss1) ;

Iniciar sesión para comentar.

Categorías

Más información sobre Live Scripts and Functions en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by