[英]Finding corresponding value of the first column from second column in an array by using efficient method? MATLAB
我有两列的数组。
第一列是时间,第二列是速度。
我正在使用此代码从第二列中找到相应的时间值。 这段代码可以正常工作,但是会使我的仿真变慢。
有人可以建议这样做的有效方法吗?
t=T'; % Time is in seconds
vv=vel'; % Speed is in meter per seconds
data= [t, vv]; % both time and velocity in one array
v2 = data(dsearchn(data(:,1),t2),2);
示例数组:
0.0 0.0
2.3 0.9
3.2 1.2
4.0 1.5
4.6 1.8
5.1 2.0
5.6 2.1
6.0 2.3
6.5 2.5
6.9 2.6
7.2 2.8
7.6 2.9
7.9 3.0
8.2 3.2
8.5 3.3
8.8 3.4
9.1 3.5
9.4 3.6
9.7 3.7
10.0 3.8
10.2 3.9
10.5 4.0
10.7 4.1
11.0 4.2
11.2 4.3
尝试类似:
matrix = zeros(length(Time_Vector),2); % initialize zero matrix
matrix(:,1) = Time_Vector; %fill first column with time vector
matrix(:,2) = Speed_Vector; %fill second column with speed vector
matrix(5,:) %displays the fifth row in Command Window as (Time , Speed)
然后用您想要的任何值修改值5
如注释中所述,您需要定义如果矩阵中未包含时间(数据的第一列)的“选择”,您将要发生的情况。
这是几个选项示例。 注意,我将data
作为样本的nx2矩阵。
仅完全匹配:如果choice
中没有data
则结果为空。
results = data(data(:,1) == choice,2)
用法示例:
>> choice = 2.3; %Time contained in data
>> results = data(data(:,1) == choice,2)
results =
0.9000
>> choice = 2.31; %Time NOT contained in data
>> results = data(data(:,1) == choice,2)
results =
Empty matrix: 0-by-1
最接近的匹配项 :返回最接近的匹配项
result = data(dsearchn(data(:,1),choice),2)
用法示例:
>> choice = 2.3; %Time contained in data
>> result = data(dsearchn(data(:,1),choice),2)
result =
0.9000
>> choice = 2.31; %Time NOT contained in data
>> result = data(dsearchn(data(:,1),choice),2)
result =
0.9000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.