MATLAB:插值向量 [英] MATLAB: interpolate vector

查看:86
本文介绍了MATLAB:插值向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MATLAB中内插向量?

How can I interpolate a vector in MATLAB?

例如,我有以下矩阵:

M=    
 1 10  
 2 20  
 3 30  
 4 40  

M的第一列表示x坐标的独立参数,而M的第二列表示输出或y坐标.

The first column of M denotes the independent parameter of x coordinate while the second column of M denotes the output or y coordinate.

我还有以下输入向量:

a =
 2.3  
 2.1  
 3.5  

对于每个a值,我希望确定输出插值结果将是什么.在这种情况下,给定a,我希望返回

For each value of a, I wish to determine what the output interpolated result would be. In this case, given a, I wish to return

23   
21   
35

推荐答案

这是修改后问题的答案,即如何插值"

Here's the answer to the question after the edit, i.e. "how to interpolate"

您要使用 interp1

M = [1 10;2 20;3 30;4 40];
a = [2.3;2.1;3.5;1.2];

interpolatedVector = interp1(M(:,1),M(:,2),a)
interpolatedVector =
    23
    21
    35
    12


这是在向量中找到两个最接近的条目"的问题的答案,即编辑之前的原始问题.


Here's the answer to the question "find the two closest entries in a vector", i.e. the original question before the edit.

x=[1,2,3,4,5]'; %'#
a =3.3;

%# sort the absolute difference
[~,idx] = sort(abs(x-a));

%# find the two closest entries
twoClosestIdx = idx(1:2);

%# turn it into a logical array
%#   if linear indices aren't good enough
twoClosestIdxLogical = false(size(x));
twoClosestIdxLogical(twoClosestIdx) = true;
twoClosestIdxLogical =
     0
     0
     1
     1
     0

这篇关于MATLAB:插值向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆