MATLAB:使用find函数获取数组中某个值的索引 [英] MATLAB: using the find function to get indices of a certain value in an array

查看:8066
本文介绍了MATLAB:使用find函数获取数组中某个值的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个双精度数组,当我想使用find命令来搜索数组中的特定值的索引时,这将产生一个空矩阵,这不是我想要的。我假设问题在于数组读取中未显示的值和/或小数位的精度。



命令:

  peaks = find(y1 == 0.8236)

数组读取:

  y1 = 

第1列到第11列

0.2000 0.5280 0.8224 0.4820 0.8239 0.4787 0.8235 0.4796 0.8236 0.4794 0.8236

第12栏至第20栏

0.4794 0.8236 0.4794 0.8236 0.4794 0.8236 0.4794 0.8236 0.4794

输出:

 峰值= 

空矩阵:1-by-0

命令

 格式short 

但我想这只会截断显示的值,而不是数组中的实际值。

如何使用find命令给出数组索引?

格式short 和格式long 只是改变显示的格式,而不是实际的格式因此,如果使用类似于 y1 = rand(100,...)创建 y1 1),你想用 find y1 中找到特定元素,你需要知道你正在寻找的元素的精确值浮点双精度 - 这取决于你的应用程序可能是不平凡的。当然, peaks = find(y1 == 0.8236)将返回空矩阵,如果 y1 只包含 0.823622378 ...



那么,如何解决这个问题呢?这取决于你的应用程序。一种方法是将 y1 中的所有值截断为您希望使用的给定精度。有趣的是,有关此主题的SO matlab问题吸引了两个好的答案12小时前,请参阅这里为更多。



如果你决定走这条路线,我会推荐这样的:

  a = 1e-4%#定义精度等级
y1Round = round((1 / a)* y1); %#舍入到适当的精度,并以整数形式离开y1 $ b $ Index = find(y1Round == SomeValue); %#执行查找操作

请注意,我使用 find 命令与 y1Round 整数形式。这是因为在使用浮点数double时整数被精确地存储,所以你不需要担心浮点数的精度。



这个问题的一个替代方法是使用 find ,并加上一些容错,例如:

  Index = find(abs(y1  -  SomeValue)< Tolerance); 

您选择的路径取决于您。然而,在采用这两种方法之前,我会仔细看看你的应用程序,看看它是否可以用某种方式重新表达,这样你就不需要从一组实数。这将是最理想的结果。编辑:在这个问题的另外两个答案提倡的代码是比我的第二种方法更整齐 - 所以我已经改变了它。


I have made an array of doubles and when I want to use the find command to search for the indices of specific values in the array, this yields an empty matrix which is not what I want. I assume the problem lies in the precision of the values and/or decimal places that are not shown in the readout of the array.

command:

peaks=find(y1==0.8236)

array readout:

y1 =

Columns 1 through 11

0.2000    0.5280    0.8224    0.4820    0.8239    0.4787    0.8235    0.4796    0.8236    0.4794    0.8236

Columns 12 through 20

0.4794    0.8236    0.4794    0.8236    0.4794    0.8236    0.4794    0.8236    0.4794

output:

peaks =

Empty matrix: 1-by-0

I tried using the command

format short

but I guess this only truncates the displayed values and not the actual values in the array.

How can I used the find command to give an array of indices?

解决方案

By default, each element of a numerical matrix in Matlab is stored using floating point double precision. As you surmise in the question format short and format long merely alter the displayed format, rather than the actual format of the numbers.

So if y1 is created using something like y1 = rand(100, 1), and you want to find particular elements in y1 using find, you'll need to know the exact value of the element you're looking for to floating point double precision - which depending on your application is probably non-trivial. Certainly, peaks=find(y1==0.8236) will return the empty matrix if y1 only contains values like 0.823622378...

So, how to get around this problem? It depends on your application. One approach is to truncate all the values in y1 to a given precision that you want to work in. Funnily enough, a SO matlab question on exactly this topic attracted two good answers about 12 hours ago, see here for more.

If you do decide to go down this route, I would recommend something like this:

a = 1e-4 %# Define level of precision
y1Round = round((1/a) * y1); %# Round to appropriate precision, and leave y1 in integer form
Index = find(y1Round == SomeValue); %# Perform the find operation

Note that I use the find command with y1Round in integer form. This is because integers are stored exactly when using floating point double, so you won't need to worry about floating point precision.

An alternative approach to this problem would be to use find with some tolerance for error, for example:

Index = find(abs(y1 - SomeValue) < Tolerance);

Which path you choose is up to you. However, before adopting either of these approaches, I would have a good hard look at your application and see if it can be reformulated in some way such that you don't need to search for specific "real" numbers from among a set of "reals". That would be the most ideal outcome.

EDIT: The code advocated in the other two answers to this question is neater than my second approach - so I've altered it accordingly.

这篇关于MATLAB:使用find函数获取数组中某个值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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