필터 지우기
필터 지우기

Index of the row

조회 수: 1 (최근 30일)
Imner Renmi
Imner Renmi 2016년 7월 8일
댓글: Star Strider 2016년 7월 8일
Hi,
I have a 5000 by 1 vector and I have a scalar, which is NOT part of the vector. I want to find the row number of the closest value to the scalar in the vector. The vector contains both positive and negative numbers and the scalar can be positive and negative too.
Anyone an idea of how to code this in Matlab.
Many thanks

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 8일
편집: Azzi Abdelmalek 2016년 7월 8일
[~,idx]=min(abs(your_vector-threshold))
  댓글 수: 1
Imner Renmi
Imner Renmi 2016년 7월 8일
Just to be sure, it will also work well for the negative values right?

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


Star Strider
Star Strider 2016년 7월 8일
This workd:
vector = randi([-99,99], 100, 1); % Create Data
scalar = 100*(rand-0.5); % Create Data
[vs,idx] = sort(vector); % Sort
q = find(vs <= scalar, 1, 'last'); % Find Lowest Close Value
[q2,ixm] = min(vs([q, q+1])); % Mininmum Distance
nearest = vs(q+ixm-1) % Result
  댓글 수: 2
Imner Renmi
Imner Renmi 2016년 7월 8일
What are 'vs' and 'idx'?
Star Strider
Star Strider 2016년 7월 8일
The ‘vs’ array is the sorted ‘vector’. The ‘idx’ vector are the original indices of the elements of the sorted vector. If you want the index instead of the value for ‘nearest’, that assignment becomes:
nearest_idx = idx(q+ixm-1)

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

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by