# Largest value near a 0

조회 수: 1(최근 30일)
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.

### 채택된 답변

James Tursa 2 Jul 2017
편집: James Tursa 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.

#### 댓글 수: 3

Salvatore Lacava 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 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 3 Jul 2017
Thanks! Now it is really clear

로그인 to comment.

### 추가 답변(1개)

Image Analyst 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.