Reputation: 1
If we have two vertical arrays one is v0 and the other is v1, and we need to prompt the user to enter a voltage value in volts. Using that value, find the closest voltage in v0 and display in a clearly labeled manner the distance value corresponding to that voltage. If there are two equally close, display the first one. How do i get this Thank you in advance
Upvotes: 0
Views: 83
Reputation: 124573
Here is an example:
%# data
v0 = randn(100,1);
v1 = rand(100,1);
%# prompt user for input
val = input('Enter a voltage value in volts : ', 's');
val = str2double(val);
if isnan(val), return, end
%# find closest match
[dist,ind] = min( abs(v0-val) );
fprintf('Closest voltage = %g\n', v0(ind))
fprintf('Distance to closest voltage = %g\n', dist)
fprintf('The closest voltage was found at position %d\n', ind)
A sample run:
Enter a voltage value in volts : 0.54
Closest voltage = 0.548403
Distance to closest voltage = 0.00840294
The closest voltage was found at position 31
With a bit more effort, you can create a decent looking GUI for this application.. I will leave that to you as an exercise :)
Upvotes: 3