How to find X value of given Y close to zero ?

7 ビュー (過去 30 日間)
Terence
Terence 2020 年 12 月 9 日
コメント済み: Terence 2020 年 12 月 10 日
Hello!
My question is How to find the value of Y more close to zero first (note Y consists of both positive and negative numbers) and then find the corresponding X value from a matrix ? Many thanks in advance.
For example: 6x6 matrix
338.00 339.00 340.00 341.00 342.00 343.00
1.00 -100.00 -100.00 -100.00 -100.00 -100.00 -100.00
2.00 100.00 100.00 100.00 100.00 100.00 100.00
3.00 0.28 0.12 -0.05 -0.21 -0.38 -0.55
4.00 0.28 0.12 -0.05 -0.21 -0.38 -0.55
5.00 8.21 8.24 8.26 8.28 8.30 8.32
6.00 8.21 8.24 8.26 8.28 8.30 8.32
To find the value closest to 0, which is -0.05 corresonding to the value 340. 340 is the output value.

採用された回答

dpb
dpb 2020 年 12 月 9 日
[~,ix]=min(abs(m),[],'all','linear');
[~,j]=ind2sub(size(m),ix);
>> o(j)
ans =
340
>>
  1 件のコメント
Terence
Terence 2020 年 12 月 9 日
It works. Many thanks!

サインインしてコメントする。

その他の回答 (1 件)

Jon
Jon 2020 年 12 月 9 日
編集済み: Jon 2020 年 12 月 9 日
Here's another approach that is maybe more obvious to understand
x = [338.00 339.00 340.00 341.00 342.00 343.00]
data = [ ...
-100.00 -100.00 -100.00 -100.00 -100.00 -100.00
100.00 100.00 100.00 100.00 100.00 100.00
0.28 0.12 -0.05 -0.21 -0.38 -0.55
0.28 0.12 -0.05 -0.21 -0.38 -0.55
8.21 8.24 8.26 8.28 8.30 8.32
8.21 8.24 8.26 8.28 8.30 8.32]
% find column locations of minimum
[~,icol] = min(abs(data)) % columns correspond to x values
% select the x value corresponding to the column where the minimum was found
% just need the first instance if there are more than one
xmin = x(icol(1))
  10 件のコメント
dpb
dpb 2020 年 12 月 10 日
Glad to help...teaching as well as "just" answers is the goal of many of us here...
Terence
Terence 2020 年 12 月 10 日
Glad to see my question raised discussion! Thanks for helping! Merry Xmas!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by