Interpolating the indexes of values in a Vector
6 次查看(过去 30 天)
显示 更早的评论
hi
i have a Vector lets say a = [2 4 6 8 10 12 14 16]; and i have a value "b" that can change.
I want to write a code that compares the value "b" to every value in "a" starting from index 1 and going to index 8 in this example.
When the value "b" finds a value in "a" so that b >= a(:) and b < a( :+1) . The algorithm will give me the index "c" of that value wich can also be interpolated.
As an example lets say b = 6, then the algorithm will give me c = 3, because the index of 6 in "a" is 3.
but if b = 5 then the algorithm should give me c = 2.5. even tho 5 doesnt exist in "a" but we can know the index with interpolation
and if b = 4.5 then the algorithm should give me c = 2.25
if b = 14.3 then the algorithm should give me c = 7.15 etc...
How can i do this?
Thank you.
0 个评论
采纳的回答
Star Strider
2023-9-19
a = [2 4 6 8 10 12 14 16];
k = 1:numel(a);
interpc = @(b) interp1(a,k,b);
bvector = [4.5 5 6 14.3];
c = interpc(bvector);
Result = [bvector; c]
.
4 个评论
更多回答(1 个)
Konrad
2023-9-19
Hi,
a = [2 4 6 8 10 12 14 16];
b = [6, 5, 4.5, 14.3];
interp1(a,1:numel(a),b,'linear') % 'linear' is also the default
Best, Konrad
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!