フィルターのクリア

finding nearest number in matrices

2 ビュー (過去 30 日間)
sajad
sajad 2014 年 7 月 14 日
コメント済み: sajad 2014 年 7 月 14 日
hi I have 2 matrices A and B.
A=[0 0.375 0.405 0.452 0.500 0.530 0.577 0.623 0.639 0.670 0.701 0.717 0.748 0.779]
B=0:01:end
I want to find nearest number of A to 0.1 and then to 0.2 and then to 0.3 and ...
in this case the nearest numbers to 0.1 and 0.2 is 0.but I want a program that find the nearest number to 0.1 and put that number away and then find the nearest number to 0.2 and so on.
can you help me?
  1 件のコメント
Jan
Jan 2014 年 7 月 14 日
編集済み: Jan 2014 年 7 月 14 日
What have you tried so far?
"B = 0:01:end" is no valid Matlab syntax. Please edit the question and replace it by what you really want.

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

採用された回答

Image Analyst
Image Analyst 2014 年 7 月 14 日
What's the purpose of B? What is "end"?
Anyway, using A, try this:
clc;
A=[0 0.375 0.405 0.452 0.500 0.530 0.577 0.623 0.639 0.670 0.701 0.717 0.748 0.779]
for k = 1 : length(A)
[~, nearestIndex(k)] = min(abs(A - k/10));
end
% Display in command window:
nearestIndex
  5 件のコメント
Image Analyst
Image Analyst 2014 年 7 月 14 日
k is an index. Indexes can't start from 0 since they have to be integers starting at 1. However you can make another variable that is just k-1 if you want.
But anyway, that code doesn't use B like my latest code, so it's not right anyway.
sajad
sajad 2014 年 7 月 14 日
I used your first code.
thanks

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

その他の回答 (1 件)

Jan
Jan 2014 年 7 月 14 日
編集済み: Jan 2014 年 7 月 14 日
A = [0 0.375 0.405 0.452 0.500 0.530 0.577 0.623 0.639 0.670 0.701 0.717 ...
0.748 0.779]
B = 0:0.1:1; % Did you menat this?!
[value, index] = min(abs(bsxfun(@minus, A, B')))
  1 件のコメント
sajad
sajad 2014 年 7 月 14 日
No. thanks but previous answer is the thing I want.
I explained the exact thing in comment
thanks again

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

カテゴリ

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