필터 지우기
필터 지우기

How to search and find nearest data in the given array

조회 수: 4 (최근 30일)
Triveni
Triveni 2022년 6월 4일
답변: Voss 2022년 6월 4일
I have an array (Matt J attached)
I have n = 70; or any other number. SP = 998.45 or any other number from column2. I want to search and find corresponding row in column2 contains 998.45 or other number with nearest value of column1 in finded rows with n =70 or any other number. Please help me.
70 987 984.65 0.1 139.72
43 987.05 984.6 0.19 89.81
70 987.05 984.7 0.1 139.72
70 987.05 984.7 0.1 139.72
43 987.1 984.65 0.18 89.8
70 987.1 984.75 0.1 139.72
43 987.15 984.7 0.18 89.8
70 987.15 984.8 0.1 139.72
43 987.2 984.75 0.18 89.8
70 987.2 984.85 0.1 139.72
43 987.25 984.8 0.18 89.8
70 987.25 984.9 0.1 139.72
43 987.3 984.85 0.18 89.8
...
  댓글 수: 2
dpb
dpb 2022년 6월 4일
Sorry, the explanation is too disjointed to undersaand.
What would your result be expected to be for the above inputs and, how, precisely would that have been determined to be the correct answer?
Triveni
Triveni 2022년 6월 4일
There are three rows containing 998.45 in column no. 2.
In these three rows, I want to find nearest value of 70.

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

채택된 답변

Voss
Voss 2022년 6월 4일
load('array.mat')
n = 70;
SP = 998.45;
% find the rows where column 2 of array is SP
matching_rows = find(array(:,2) == SP)
matching_rows = 3×1
541 542 543
% find the index of the value nearest to n
% in column 1, among those rows of array
[~,idx] = min(abs(array(matching_rows,1) - n))
idx = 2
% get the value
result = array(matching_rows(idx),1)
result = 62

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by