selecting an element of a vector
10 ビュー (過去 30 日間)
古いコメントを表示
How to select an element of a vector which is nearest to the given value xp.
Suppose we have a vector
x=[1 1.05 1.1 1.15 1.2 1.25]
a) if xp=1.18, the output should be 1.2 (which is nearest to 1.18)
b) if xp=1.12, the output should be 1.1 (which is nearest to 1.12)
2 件のコメント
madhan ravi
2018 年 10 月 23 日
編集済み: madhan ravi
2018 年 10 月 23 日
b) if xp=1.12, the output should be 1.1 (which is nearest to 1.12)
nearest to 1.12 is 1.15 ?? not 1.1 , whats the logic behind it ,explain to understand
採用された回答
KALYAN ACHARJYA
2018 年 10 月 23 日
編集済み: KALYAN ACHARJYA
2018 年 10 月 23 日
x=[1 1.05 1.1 1.15 1.2 1.25];
xp=input('Enter the xp value: ');
[d, idx]=min(abs(x-xp));
fprintf('The output is%.2f',x(idx));
Command Window
Enter the xp value: 1.18
The output is1.20
2 件のコメント
KALYAN ACHARJYA
2018 年 10 月 23 日
編集済み: KALYAN ACHARJYA
2018 年 10 月 23 日
idx is giving the index position of x, which is close to xp
if idx=1, that means x(idx)=x(1)=1 (First element)
d gives the minimum difference of x-xp, abs absolute value (+ve)
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!