closest value to zero excluding the first one?

조회 수: 8 (최근 30일)
Ali Almakhmari
Ali Almakhmari . 2023년 1월 12일
댓글: Bruno Luong . 2023년 1월 12일
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 = find(diff(dis_y>=0),1);

댓글을 달려면 로그인하십시오.

채택된 답변

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일
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
  댓글 수: 2
Ali Almakhmari
Ali Almakhmari 2023년 1월 12일
I = find(diff(dis_y>=0),1);

댓글을 달려면 로그인하십시오.

카테고리

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