フィルターのクリア

Test value equality within a range of values

1 回表示 (過去 30 日間)
Vishan Gupta
Vishan Gupta 2018 年 9 月 29 日
編集済み: Bruno Luong 2018 年 9 月 29 日
I have a=0.3, I have a vector jki=0:0.00065:0.65 (its a 1x1001 vector). I want to compare each value of jki to a, if that is true, display something. I've tried searching online but don't really understand still how to do it. This is what my guess would be for the code, but it doesn't work:
for k=1:size(jki)
if jki(k)==a
disp('something');
end
end

回答 (2 件)

Adam Danz
Adam Danz 2018 年 9 月 29 日
If your goal is to do something if a is in jki
if ismember(a,jki)
DoSomething
end
  2 件のコメント
Vishan Gupta
Vishan Gupta 2018 年 9 月 29 日
Doesn't work because a is 0.3, the closest value to 0.3 IN jki is 0.2996 and 0.3003, the vector never lands on exactly 0.300 which is the problem I am having.
Walter Roberson
Walter Roberson 2018 年 9 月 29 日
I do not see the problem? You say, "I want to compare each value of jki to a" and you show an equality comparison in your sample code. If 0.3 is not in jki then 0.3 is not in jki.
Is the question to determine where in jki that a falls if you treat the entries of jki to be edges? If so then see the first output of discretize() or see the second output of histc() or the third output of histcounts()

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


Bruno Luong
Bruno Luong 2018 年 9 月 29 日
編集済み: Bruno Luong 2018 年 9 月 29 日
Is this is what you want?
jki = 0:0.00065:0.65;
a = 0.3;
[~,loc] = histc(a,jki);
fprintf('%g is in the range (%g,%g)\n', a, jki(loc+[0 1]))

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by