Academia
Academia

Reputation: 4124

nearest element MATLAB

I want to find the index of the nearest element in x=0:0.1:pi/2 to a given number z=0.65. I did already this, but I want something better: [C,I]=min(abs(x-z)). I is the index of the nearest element.

Upvotes: 4

Views: 4700

Answers (1)

Sam Roberts
Sam Roberts

Reputation: 24127

[C, I] = min(abs(x-z)) is what I would do - I can't think of an improvement on that. [~, I] = min(abs(x-z)) would allow you to not create an unnecessary variable C, if you regard that as an improvement.

Upvotes: 6

Related Questions