Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 240348

Submitted on 5 May 2013 by andrea84

Correct

44Size
Leading solution size is 26.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%%
x = 3:10;
y = 5.6;
[v,i] = nearVal(x,y);
assert(v == 6 && i == 4)
a =
    0.4000
b =
     4
a =
     6
2
Pass
 
%%
x = [1 1000];
y = 5.6;
[v,i] = nearVal(x,y);
assert(v == 1 && i == 1)
a =
    4.6000
b =
     1
a =
     1
3
Pass
 
%%
x = 1:500;
y = 3:3:9;
[v,i] = nearVal(x,y);
assert(all(v == y & i == y))
a =
     0
b =
     3
a =
     3
a =
     3     0
b =
     3     6
a =
     3     6
a =
     3     6     0
b =
     3     6     9
a =
     3     6     9
4
Pass
 
%%
x = 1;
y = 1:100;
[v,i] = nearVal(x,y);
assert(all(v == 1 & i == 1))
a =
     0
b =
     1
a =
     1
a =
     1     1
b =
     1     1
a =
     1     1
a =
     1     1     2
b =
     1     1     1
a =
     1     1     1
a =
     1     1     1     3
b =
     1     1     1     1
a =
     1     1     1     1
a =
     1     1     1     1     4
b =
     1     1     1     1     1
a =
     1     1     1     1     1
a =
     1     1     1     1     1     5
b =
     1     1     1     1     1     1
a =
     1     1     1     1     1     1
a =
     1     1     1     1     1     1     6
b =
     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     7
b =
     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     8
b =
     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     9
b =
     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1    10
b =
     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1    11
b =
     1     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1     1    12
b =
     1     1     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1     1     1    13
b =
     1     1     1     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1     1     1     1    14
b =
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1    15
b =
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
a =
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Column 17
    16
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Column 17
     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Column 17
     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 18
     1    17
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 18
     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 18
     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 19
     1     1    18
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 19
     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 19
     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 20
     1     1     1    19
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 20
     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 20
     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 21
     1     1     1     1    20
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 21
     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 21
     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 22
     1     1     1     1     1    21
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 22
     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 22
     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 23
     1     1     1     1     1     1    22
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 23
     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 23
     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 24
     1     1     1     1     1     1     1    23
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 24
     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 24
     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 25
     1     1     1     1     1     1     1     1    24
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 25
     1     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 25
     1     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 26
     1     1     1     1     1     1     1     1     1    25
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 26
     1     1     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 26
     1     1     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 27
     1     1     1     1     1     1     1     1     1     1    26
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 27
     1     1     1     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 27
     1     1     1     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 28
     1     1     1     1     1     1     1     1     1     1     1    27
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 28
     1     1     1     1     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 28
     1     1     1     1     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 29
     1     1     1     1     1     1     1     1     1     1     1     1    28
b =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
  Columns 17 through 29
     1     1     1     1     1     1     1     1     1     1     1     1     1
a =
  Columns 1 through 16
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
 ...
5
Pass
 
%%
x = (3:5).^2;
y = 6:16;
[v,i] = nearVal(x,y);
assert(isequal(v,[9 9 9 9 9 9 9 16 16 16 16]) && isequal(i,[1 1 1 1 1 1 1 2 2 2 2]))
a =
     3
b =
     1
a =
     9
a =
     9     2
b =
     1     1
a =
     9     9
a =
     9     9     1
b =
     1     1     1
a =
     9     9     9
a =
     9     9     9     0
b =
     1     1     1     1
a =
     9     9     9     9
a =
     9     9     9     9     1
b =
     1     1     1     1     1
a =
     9     9     9     9     9
a =
     9     9     9     9     9     2
b =
     1     1     1     1     1     1
a =
     9     9     9     9     9     9
a =
     9     9     9     9     9     9     3
b =
     1     1     1     1     1     1     1
a =
     9     9     9     9     9     9     9
a =
     9     9     9     9     9     9     9     3
b =
     1     1     1     1     1     1     1     2
a =
     9     9     9     9     9     9     9    16
a =
     9     9     9     9     9     9     9    16     2
b =
     1     1     1     1     1     1     1     2     2
a =
     9     9     9     9     9     9     9    16    16
a =
     9     9     9     9     9     9     9    16    16     1
b =
     1     1     1     1     1     1     1     2     2     2
a =
     9     9     9     9     9     9     9    16    16    16
a =
     9     9     9     9     9     9     9    16    16    16     0
b =
     1     1     1     1     1     1     1     2     2     2     2
a =
     9     9     9     9     9     9     9    16    16    16    16