フィルターのクリア

Find multiple closest values in a matrix

2 ビュー (過去 30 日間)
SA Yoganathan
SA Yoganathan 2015 年 8 月 25 日
コメント済み: SA Yoganathan 2015 年 8 月 26 日
Hello everyone,
I have a matrix
a = [0.00000 -0.15593 -0.23176 -0.37512 -0.50185 -0.55736 -0.75122 -0.64952 -0.50185 -0.37520 -0.24170 -0.15590 -0.00000 0.15593 0.24762 0.37512 0.50185 0.68516 0.75120]
While reading the elements of matrix 'a', I want to do 'for loop' whenever the element value is close to 0, -0.25, +0.25, -0.5, 0.5, -0.75 and 0.75.
Thanks
  1 件のコメント
Walter Roberson
Walter Roberson 2015 年 8 月 25 日
You have two -0.50185, each of which is "closest" to -0.5 . What is your desired behaviour in that case?

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

採用された回答

Walter Roberson
Walter Roberson 2015 年 8 月 25 日
ua = unique(a);
closest_a = ua(interp1(ua,1:length(ua), [ 0, -0.25, +0.25, -0.5, 0.5, -0.75, 0.75], 'nearest'));
for K = 1 : length(a)
if ismember(a(K), closest_a)
... do whatever ...
end
end
If there were no duplicates then the code would be shorter and it would not be necessary to test ismember() inside the loop.
  1 件のコメント
SA Yoganathan
SA Yoganathan 2015 年 8 月 26 日
Thank you very much

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by