Cody

# Problem 176. Nearest

Solution 535328

Submitted on 25 Nov 2014 by goc3
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
%% x = 3:10; y = 5.6; [v,i] = nearVal(x,y); assert(v == 6 && i == 4)

t = 1 ind = 4 v = 6

2   Pass
%% x = [1 1000]; y = 5.6; [v,i] = nearVal(x,y); assert(v == 1 && i == 1)

t = 1 ind = 1 v = 1

3   Pass
%% x = 1:500; y = 3:3:9; [v,i] = nearVal(x,y); assert(all(v == y & i == y))

t = 1 ind = 3 v = 3 t = 1 ind = 3 6 v = 3 6 t = 1 ind = 3 6 9 v = 3 6 9

4   Pass
%% x = 1; y = 1:100; [v,i] = nearVal(x,y); assert(all(v == 1 & i == 1))

t = 1 ind = 1 v = 1 t = 1 ind = 1 1 v = 1 1 t = 1 ind = 1 1 1 v = 1 1 1 t = 1 ind = 1 1 1 1 v = 1 1 1 1 t = 1 ind = 1 1 1 1 1 v = 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 v = 1 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 1 v = 1 1 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 1 1 v = 1 1 1 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 1 1 1 v = 1 1 1 1 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 1 1 1 1 v = 1 1 1 1 1 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 1 1 1 1 1 v = 1 1 1 1 1 1 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 1 1 1 1 1 1 v = 1 1 1 1 1 1 1 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 1 1 1 1 1 1 1 v = 1 1 1 1 1 1 1 1 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 v = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 v = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 t = 1 ind = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 v = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 t = 1 ind = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Column 17 1 v = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Column 17 1 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = 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 v = 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 t = 1 ind = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 30 1 1 1 1 1 1 1 1 1 1 1 1 1 1 v = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 30 1 1 1 1 1 1 1 1 1 1 1 1 1 1 t = 1 ind = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 31 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 v = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 31 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 t = 1 ind = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 v = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 t = 1 ind = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Column 33 1 v = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Column 33 1 t = 1 ind = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 33 through 34 1 1 v = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 33 through 34 1 1 t = 1 ind = Columns 1 through 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 17 through 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Columns 33 through 35 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]))

t = 1 ind = 1 v = 9 t = 1 ind = 1 1 v = 9 9 t = 1 ind = 1 1 1 v = 9 9 9 t = 1 ind = 1 1 1 1 v = 9 9 9 9 t = 1 ind = 1 1 1 1 1 v = 9 9 9 9 9 t = 1 ind = 1 1 1 1 1 1 v = 9 9 9 9 9 9 t = 1 ind = 1 1 1 1 1 1 1 v = 9 9 9 9 9 9 9 t = 1 ind = 1 1 1 1 1 1 1 2 v = 9 9 9 9 9 9 9 16 t = 1 ind = 1 1 1 1 1 1 1 2 2 v = 9 9 9 9 9 9 9 16 16 t = 1 ind = 1 1 1 1 1 1 1 2 2 2 v = 9 9 9 9 9 9 9 16 16 16 t = 1 ind = 1 1 1 1 1 1 1 2 2 2 2 v = 9 9 9 9 9 9 9 16 16 16 16

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!