-4

am asking if there is a function in matlab which finds the first max and first min numbers compered to a number in an array

example :

if we have an array A = [1 2 3 4 5 6 7 8 9];

and the number is x=3.4;

How can we find whith a function (and not with a loop) and store the fist min and firts max of x in variables min_x max_x ?

desirable results:

min_x = 3

max_x = 4

  • I'm not sure if I understand correctly, do you want to find the largest and smallest digit in a decimal number? – mike Feb 12 '14 at 15:54

2 Answers2

3

Use logical indexing to select the entries of A smaller (larger) than x, and then use max (min):

max(A(A-x<0))
min(A(A-x>0))

I'm interpreting "next highest/smallest" as the closest numbers to x in A, from above and from below.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
0

First you'll probably want to convert the decimal to an array. Here are some suggested ways that you can do this. After you have an array of your digits, you should be able to juse use MATLAB's built in max() and min() functions.

Community
  • 1
  • 1
mike
  • 1,318
  • 3
  • 21
  • 41