interp1 function not working properly

조회 수: 102 (최근 30일)
Dominik Stolfa
Dominik Stolfa 2024년 11월 26일 14:23
댓글: Dominik Stolfa 2024년 11월 30일 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
Dominik Stolfa
Dominik Stolfa 2024년 11월 26일 20:12

Thank you very much. Sometimes I can be pretty dense.

Image Analyst
Image Analyst 2024년 11월 26일 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:

댓글을 달려면 로그인하십시오.

채택된 답변

Subhajyoti
Subhajyoti 2024년 11월 26일 14:39
편집: Subhajyoti 2024년 11월 26일 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)
ans = 1×130
1.0000 1.8837 2.7674 3.6512 4.5349 5.4186 6.3023 7.1860 8.0698 8.9535 9.8372 10.7209 11.6047 12.4884 13.3721 14.2558 15.1395 16.0233 16.9070 17.7907 18.6744 19.5581 20.4419 21.3256 22.2093 23.0930 23.9767 24.8605 25.7442 26.6279
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Refer to the following MathWorks Documentation to know more about 'linspace':
  댓글 수: 4
Stephen23
Stephen23 2024년 11월 26일 18:16
편집: Stephen23 2024년 11월 26일 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
Dominik Stolfa 2024년 11월 26일 19:50
편집: Dominik Stolfa 2024년 11월 26일 19:50
Linspace doesn’t seem to work for my case: interp1=(1:115,x(1:115),1:115/130:115) I think.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2024년 11월 26일 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
Walter Roberson
Walter Roberson 2024년 11월 28일 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)
ans = 115
whos A
Name Size Bytes Class Attributes A 1x77 616 double
B = linspace(1,115,77);
[~,idx] = max(abs(A-B));
A(idx), B(idx), A(idx)-B(idx)
ans = 1
ans = 1
ans = 0
Dominik Stolfa
Dominik Stolfa 2024년 11월 30일 10:32
I will try to use the extreme case. If I wanted to increment every 1/2 and I could not because of rounding error and therefore used step (1-1)/2-1), the results won’t be same anymore.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by