## Largest value near a 0

Salvatore Lacava

### Salvatore Lacava (view profile)

님이 질문을 제출함. 2 Jul 2017
최근 활동 Salvatore Lacava

### Salvatore Lacava (view profile)

님이 댓글을 추가함. 3 Jul 2017
James Tursa

### James Tursa (view profile)

님이 답변을 채택함.
Hi all! I got stuck in an assignment, that is the following: write a function that takes as input a vector and outputs the largest adjacent value near a 0. So that [ 1 4 6 2 8 0 7 3 4 6 0 1] becomes y = 8. I wrote the following function
function y = nearZero(x)
for i = 1:length(x);
b = [];
if x(i) == 0 && x(i) ~= x(end)
b(i) = x(i-1)
b(i+1) = x(i+1)
else
b(i) = NaN;
end
end
y = b
end
But I get a vector of 0s. Could you give me a hint in how to make this code work? Thanks

로그인 to comment.

## 답변 수: 2

James Tursa

님의 답변 2 Jul 2017
James Tursa

### James Tursa (view profile)

님이 편집함. 2 Jul 2017
채택된 답변

If I understand the assignment correctly, maybe you can use this as an outline:
y = -inf;
for i = 1:length(x)
% Write code here to see if x(i) is near a 0
% Have the result of this code be a logical variable called e.g. nearzero
if( nearzero )
y = max(y,x(i));
end
end
To write the code for nearzero, you will need to check if x(i-1) == 0 or if x(i+1) == 0. But you can't blindly do this, since this will not work for x(1) and x(end) because they are on the ends. So you will need to put in some checks to see if it is a start or end element to know which elements nearby you can check.

Salvatore Lacava

### Salvatore Lacava (view profile)

2 Jul 2017
Hi James, thanks for you reply. I tried to follow your lead, but the way I implemented it does not process correctly the first and the last value in the array, and if I change the 4th line with "i = 1:length(x)-1" I get the error 'Subscript indices must either be real positive integers or logicals.'. Could you give me some feedback on how to work around this issue? Thanks
function y = nearZero(x)
y = -inf;
for i = 2:length(x)-1
% Write code here to see if x(i) is near a 0
if i == 1 && x(i+1) == 0
nearzero(i) = x(i)
elseif i == length(x) && x(i-1) == 0
nearzero(i) = x(i)
elseif x(i-1) == 0 || x(i+1) == 0
nearzero(i) = x(i)
else
nearzero(i) = NaN
end
end
y = max(nonzeros(nearzero))
end
James Tursa

### James Tursa (view profile)

3 Jul 2017
That's not going to work because if the first element does not pass that first if check, you will fall down into the third if check and try to access x(0) which will generate an error. To get around that issue, separate your index checking from your value checking. That way you will not fall into the third if check when it is not appropriate. E.g., maybe start your function with some error checking and size checking:
if( ~isvector(x) )
error('nearZero needs a vector input');
end
if( numel(x) == 1 )
y = NaN; % <-- or change to whatever you want to do for a scalar input
return
end
nearzero = NaN(size(x)); % <-- Initialize nearzero to all NaN values
Then breakout your if checking. E.g., this
if i == 1 && x(i+1) == 0
nearzero(i) = x(i)
if i == 1
if x(i+1) == 0
nearzero(i) = x(i);
end
Same for the other elseif checks. That way the first element check will not inadvertently drop down into other checks that you don't want.
Finally, you won't need this piece if you initialize nearzero to NaN values like I have done:
else
nearzero(i) = NaN
Salvatore Lacava

### Salvatore Lacava (view profile)

3 Jul 2017
Thanks! Now it is really clear

로그인 to comment.

Image Analyst

### Image Analyst (view profile)

님의 답변 2 Jul 2017

Well your homework probably doesn't expect you to use image processing functions like imdilate(), which is a local max function, but anyway, it can be done in 2 lines of code if you do use it:
v = [ 1 4 6 2 8 0 7 3 4 6 0 1]
mask = imdilate(v == 0, ones(1,3)) % Find 0's and expand by 1 element in each direction.