Elements of a vector whose difference is minimum?
조회 수: 1 (최근 30일)
이전 댓글 표시
Is it possible to find the elements of a vector whose difference is minimum?
댓글 수: 0
채택된 답변
Matt J
2016년 10월 6일
편집: Matt J
2016년 10월 6일
If v is the given vector,
N=length(v);
G=abs(bsxfun(@minus,v(:),v(:).'));
G(1:N+1:end)=nan;
[i,j]=find(G==min(G(:)));
v(i), v(j)
댓글 수: 1
LauraLee Austin
2016년 10월 6일
Matt J, I like your approach, but it's missing a step which removes the difference of itself (i.e. element 2 minus element 2 equals 0)
G=abs(bsxfun(@minus,v(:),v(:).'));
a = 1:1+length(v):numel(G);
G(a) = NaN;
[i,j]=find(G==min(G(:)));
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!