interp1 function not working properly
이전 댓글 표시
When I have signal of 1:130 and I interpolate it over 1:115/130:115 it returns array of only 129 values.
댓글 수: 10
Why would you expect more than that?
size(1:115/130:115)
"interp1 function not working properly"
No, INTERP1 is working correctly.
However you did not check the data you are using, and so provided INTERP1 with exactly 129 values:
numel(1:115/130:115)
The cause is mathematics. Lets check the values by going two steps further:
V = 1:115/130:116;
X = (-4:0)+numel(V) % last five indices
V(X) % last five values
Note how element 129 has a value less than 115.
Note how element 130 has a value greater than 115.
So your step never actually reaches the value 115 (even within binary floating point precision). There is certainly no reason to expect to get 130 elements from that operation, and the mistake has absolutely nothing to do with INTERP1.
Solution: use LINSPACE.
Dominik Stolfa
2024년 11월 26일
Dominik Stolfa
2024년 11월 26일
" I went briefly through the links but I still don’t know how to fix it."
V = linspace(1,115,130)
V(130)
Or if you really want to obfuscate your code:
V = interp1([1,130],[1,115],1:130)
V(130)
V = rescale(1:130,1,115)
V(130)
V = 1+114.*(0:129)./129
V(130)
Dominik Stolfa
2024년 11월 26일
편집: Dominik Stolfa
2024년 11월 26일
Dominik Stolfa
2024년 11월 26일
Voss
2024년 11월 26일
interp1(1:115,x(1:115),linspace(1,115,130))
Dominik Stolfa
2024년 11월 26일
Image Analyst
2024년 11월 26일
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:
채택된 답변
추가 답변 (1개)
Image Analyst
2024년 11월 26일
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
"...either assign/overwrite the final number to 115"
Note that this does not change the number of elements in the vector.
"... OR concatenate the 115 onto the existing vector."
Note that in general this returns unequal spacing of the last value:
V = [1:115/130:115,115];
D = diff(V);
plot(D,'-*')
Solution: use LINSPACE.
Dominik Stolfa
2024년 11월 26일
Dominik Stolfa
2024년 11월 26일
편집: Dominik Stolfa
2024년 11월 26일
v = sym(1):sym(115)/sym(130):sym(115); disp(char(v))
Your algorithm for generating 130 values is faulty.
w = sym(0):sym(115)/sym(130):sym(115); disp(char(w))
Whereas starting from zero gives you something that ends exactly at 115 (at least when carried out to indefinite precision.)
whos v w
w has 131 values, not 130. It starts with 0, and the second element is less than 1, so if you were to discard the entries less than 1 then you would end up with only 129 values.
x = linspace(sym(1), sym(115), 130);
w1 = x(2)-x(1)
w2 = double(w1)
y1 = 1:w1:115
y2 = 1:w2:115
whos y1 y2
So if you want to use the colon operator, you need to use an increment of 38/43. Which is exactly (115-1)/(130-1)
Dominik Stolfa
2024년 11월 26일
Walter Roberson
2024년 11월 27일
Short summary:
In order to use 1:INCREMENT:115 and end up with 130 results, you need the increment to be (115-1)/(130-1)
Dominik Stolfa
2024년 11월 28일
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)
Dominik Stolfa
2024년 11월 30일
카테고리
도움말 센터 및 File Exchange에서 Number Theory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
