Elements of a vector whose difference is minimum?
2 次查看(过去 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(:)));
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!