Find Several values closest to zero

40 ビュー (過去 30 日間)
Manuel Steinbrecher
Manuel Steinbrecher 2017 年 5 月 30 日
コメント済み: Anthony Snow 2020 年 4 月 15 日
Hey all,
I have a simple vector containing my data and wanna find the index of its value closest to zero. At the moment, I am just doing:
zeroIX=dsearchn(mydata,0);
However, this only gives me the very first value. If I have for example a vector like this:
mydata= [1;2;5;0.1;0.1;0.1;2;3]
I omit all the other two values, which are exactly as far away from 0. But in this case for example, I need the index of the middle one. I read through several ideas but haven't figured out a way to find similar values close to zero. Can anyone of you help me?
Thanks in advance and best, Manuel

採用された回答

KSSV
KSSV 2017 年 5 月 30 日
Define a small fraction..say eps of your required order...
eps = 10^-2 ;
idx = mydata<=eps ;
  2 件のコメント
Stephen23
Stephen23 2017 年 5 月 30 日
編集済み: Stephen23 2017 年 5 月 30 日
@KSSV: do NOT use eps as a variable name: it has a specific meaning (the machine epsilon), so re-defining it is as confusing as re-defining pi. And because it is such an important value in computer science, it is already defined as the very important MATLAB function eps.
What you used in your example is a tolerance, but it is certainly not the machine epsilon.
Anthony Snow
Anthony Snow 2020 年 4 月 15 日
What would you input if you only want numbers in a matrix on the order of lets say 1e-2 (to keep it constant with your previous reply) set equal to zero but NOT the numbers that are negative ( ~mydata<0)

サインインしてコメントする。

その他の回答 (1 件)

Manuel Steinbrecher
Manuel Steinbrecher 2017 年 5 月 30 日
Well, I tend to forget how easy Matlab can be. Thanks for your reply.
Even though this doesn't fully do the job for me, since my problem is slightly different actually, this gave me the idea how to do it. So my actual problem is: I can have data, with several actual zeros in it (they are consecutive then!) or I can have data where I always slightly miss the zero. So I will now just do an if-check and check whether idx=mydata==0 gives any result, and if not, I just do idx = dsearchn(mydata,0).
If there is any smarter way, let me know :) Thx KSSV

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by