interp1 function not working properly
102 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dominik Stolfa
el 26 de Nov. de 2024 a las 14:23
Comentada: Dominik Stolfa
el 30 de Nov. de 2024 a las 10:32
When I have signal of 1:130 and I interpolate it over 1:115/130:115 it returns array of only 129 values.
10 comentarios
Image Analyst
el 26 de Nov. de 2024 a las 21:34
You should have already learned about quantization and truncation error when you took your college math course on linear algebra or numerical analysis. Hopefully this FAQ entry will supply your missing knowledge:
Respuesta aceptada
Subhajyoti
el 26 de Nov. de 2024 a las 14:39
Editada: Subhajyoti
el 26 de Nov. de 2024 a las 14:41
When you are creating the new range '1:115/130:115', it might not include the last point 115 due to floating-point arithmetic precision, leading to only 129 interpolated values instead of 130.
You can use 'linspace' function in MATLAB to generate linearly spaced vector. Here, in the following code snippet, I have used it to create 130 uniformly-spaced values between 1 and 115.
linspace(1, 115, 130)
Refer to the following MathWorks Documentation to know more about 'linspace':
4 comentarios
Stephen23
el 26 de Nov. de 2024 a las 18:16
Editada: Stephen23
el 26 de Nov. de 2024 a las 18:20
"Can I somehow force interp1 to include also the last number though?"
The problem has nothing to do with INTERP1. Nothing you do with INTERP1 will make any difference to how many sample points you provide it with. Solution: use LINSPACE.
"I tried using double here and there but it didn’t help."
Correct.
Dominik Stolfa
el 26 de Nov. de 2024 a las 19:50
Editada: Dominik Stolfa
el 26 de Nov. de 2024 a las 19:50
Más respuestas (1)
Image Analyst
el 26 de Nov. de 2024 a las 18:22
Like @Subhajyoti said, linspace would be the preferred way to get exactly the number of elements you want and to get it to end exactly in the number you want.
However, you said you want the last number to be 115 so there are two ways: either assign/overwrite the final number to 115, OR concatenate the 115 onto the existing vector.
v1 = linspace(1, 115, 130) % Best way.
% Using colon operator (less preferred than linspace():
v2 = 1 : (115/130) : 115 % 1 through 114.230769230769
% If you want the last value to be
% 115 instead of 114.230769230769 you can do this
v2(end) = 115;
% Or you can do this
v2 = 1 : (115/130) : 115 % 1 through 114.230769230769
v2 = [v2, 115] % Last two numbers are 114.230769230769 and 115.
9 comentarios
Walter Roberson
el 28 de Nov. de 2024 a las 18:34
I don't know what you mean by "large fractions".
It works fine for 7/5
A = 1:(7-1)/(5-1):115;
A(end)
whos A
B = linspace(1,115,77);
[~,idx] = max(abs(A-B));
A(idx), B(idx), A(idx)-B(idx)
Ver también
Categorías
Más información sobre Descriptive Statistics 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!