필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by