필터 지우기
필터 지우기

Finding a row with a certain condition

조회 수: 2 (최근 30일)
AA
AA 2016년 6월 22일
답변: Star Strider 2016년 6월 22일
Assume I have the following matrix.
733458 91 1.01510000000000 1.01680000000000 1.01490000000000 1.01520000000000 1.01585000000000
733458 112 1.01620000000000 1.01620000000000 1.01400000000000 1.01610000000000 1.01510000000000
733458 135 1.01440000000000 1.01540000000000 1.01430000000000 1.01480000000000 1.01485000000000
733458 154 1.01420000000000 1.01580000000000 1.01410000000000 1.01420000000000 1.01495000000000
733458 173 1.01550000000000 1.01570000000000 1.01490000000000 1.01560000000000 1.01530000000000
I want to find the row that contains in column one the value 733458 and that is the closest to 158 in column 2. How can I do that?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 22일
A=[733458 91 1.01510000000000 1.01680000000000 1.01490000000000 1.01520000000000 1.01585000000000
733458 112 1.01620000000000 1.01620000000000 1.01400000000000 1.01610000000000 1.01510000000000
733458 135 1.01440000000000 1.01540000000000 1.01430000000000 1.01480000000000 1.01485000000000
733458 154 1.01420000000000 1.01580000000000 1.01410000000000 1.01420000000000 1.01495000000000
733459 173 1.01550000000000 1.01570000000000 1.01490000000000 1.01560000000000 1.01530000000000]
ii=ismember(A(:,1),733458)
B=A(ii,:)
[~,jj]=min(abs(A(ii,2)-158))
out=B(jj,:)

추가 답변 (1개)

Star Strider
Star Strider 2016년 6월 22일
One approach:
x = 158;
idx = find((M(:,1) == 733458) & ((x-M(:,2)).^2) == min( (x-M(:,2)).^2));
idx =
4

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by