findnear.m

来自「计量工具箱」· M 代码 · 共 31 行

M
31
字号
function [value,position] = findnear(A,b)% PURPOSE: finds element in the input matrix (or vector) with %          value closest to the input value b % -----------------------------------------------------% USAGE: [value,pos] = findnear(A,b)%          where:  A = a matrix or vector%                  b = value% -----------------------------------------------------% RETURNS: value = nearest value%          pos   = scalar if A is a vector containing row element%                = 1 x 2 vector is A is a matrix with row,col element% -----------------------------------------------------% I don't know who wrote this oneif nargin ~= 2error('findnear: Wrong # of input arguments');end;   dist = abs(A-b);          % The minimum of the distance vector          tmp = repmat(min(dist(:)),size(dist));   [pos(1),pos(2)] = find(dist == tmp);   value = A(pos(1),pos(2));          [n,m] = size(A);          if m == 1          position = pos(1);          else          position = pos;          end;  

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?