Largest value near a 0

2 vues (au cours des 30 derniers jours)
Salvatore Lacava
Salvatore Lacava le 2 Juil 2017
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

Réponse acceptée

James Tursa
James Tursa le 2 Juil 2017
Modifié(e) : James Tursa le 2 Juil 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 commentaires
James Tursa
James Tursa le 3 Juil 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)
becomes this instead:
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 le 3 Juil 2017
Thanks! Now it is really clear

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 2 Juil 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.
largestValue = max(v(mask))

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by