closest value to zero excluding the first one?

16 次查看(过去 30 天)
If given a long vector of values. How can I find the index of the closest value to zero excluding the first value of the vector out of our list of stuff to search for
  2 个评论
Ali Almakhmari
Ali Almakhmari 2023-1-12
I am attaching an example variable and the answer your code should result in should be 175 or the 174 index.

请先登录,再进行评论。

采纳的回答

Ali Almakhmari
Ali Almakhmari 2023-1-12
"dis_y" being the variable:
I = find(diff(dis_y>=0),1);
  1 个评论
Bruno Luong
Bruno Luong 2023-1-12
That is NOT your question asks for, that is first consecutive points where 0 is cross from positive to negative.

请先登录,再进行评论。

更多回答(2 个)

Torsten
Torsten 2023-1-12
编辑:Torsten 2023-1-12
out of our list of stuff to search for
?
v = [-0.1 2 4 -0.5 8];
[~,i] = min(abs(v(2:end)))
i = 3
v(i+1)
ans = -0.5000
  3 个评论
Ali Almakhmari
Ali Almakhmari 2023-1-12
编辑:Ali Almakhmari 2023-1-12
hmm I dont think this works. For example, I attached this variable here. The answer to my question should be 175 or the 174 index. But with this code you have I do not get that.
Ali Almakhmari
Ali Almakhmari 2023-1-12
Actually I found the solution: I = find(diff(dis_y>=0),1);

请先登录,再进行评论。


Bora Eryilmaz
Bora Eryilmaz 2023-1-12
编辑:Bora Eryilmaz 2023-1-12
load('var.mat')
[~,I] = sort(abs(dis_y), 'ascend');
% Closest value to 0:
dis_y(I(1))
ans = 2.3761e-05
% Second closest value to 0:
dis_y(I(2))
ans = -0.0014

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by