如何在MATLAB中使用find()来查找小数 [英] How to use find() in MATLAB in order to find decimals

查看:1456
本文介绍了如何在MATLAB中使用find()来查找小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个有两列的表 - 一个是P_in,另一个是P_out:
$ b $ pre pre_in = table 1);
P_out = table(:,2);

我也给了P_in的一个子集,我称之为P_in2。我想找到属于P_in2的P_out的相应值。



我试过find():

  P_out2_idx = find(table (P_in2,2)); 
P_out2 = table(P_out2,idx,2);

但find()只能处理整数。我的表,但是,不显示整数,但小数。这里是表中的三行:

  -13.02 49.6 
-12.52 49.9
-12.02 50.18

所以即使舍入P_in2值也不行。 如果你的数字是整数。对于浮点数更安全的方法是使用 ismembertol
$ b $ $ $ $ $ $ $ $ $ $ $ isInP_in2 = ismembertol(P_in,P_in2,eps);
P_out2 = P_out(isInP_in2);

返回的数组 isInP_in2 是一个逻辑数组 P_in 的大小,它是 true P_in2 被找到, false 否则。

第三个参数是用于比较的容差我们采用浮点相对精度 eps )。例如,使用 find 搜索一个索引就可以完成了:

  idx = find(abs(P_in-P_in2(1))  


I am given a table with two columns - one is P_in and the other P_out:

P_in  = table(:,1);
P_out = table(:,2);

I am also given a subset of P_in, which I call P_in2. I want to find the corresponding value of P_out that belongs to P_in2.

I tried find():

P_out2_idx = find(table(P_in2,2));
P_out2     = table(P_out2,idx,2);

But find() can only deal with integers. My table however, does not show integers, but decimals. Here are three lines from the table:

-13.02    49.6
-12.52    49.9
-12.02    50.18

So even rounding the P_in2 values wouldn't work.

解决方案

Using ismember is good for finding multiple indices if your numbers are integers. For floating-point numbers a safer way is using ismembertol:

isInP_in2 = ismembertol(P_in, P_in2, eps);
P_out2 = P_out(isInP_in2);

The returned array isInP_in2 is a logical array the size of P_in, which is true at the indices where values in P_in2 are found, and false otherwise.

The third argument is the tolerance used for comparisons (here we take the floating-point relative accuracy eps). For example, searching for a single index using find would be done:

idx = find(abs(P_in - P_in2(1)) < eps);

这篇关于如何在MATLAB中使用find()来查找小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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