Largest value near a 0

조회 수: 1 (최근 30일)
Salvatore Lacava
Salvatore Lacava 2017년 7월 2일
댓글: Salvatore Lacava 2017년 7월 3일
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

채택된 답변

James Tursa
James Tursa 2017년 7월 2일
편집: James Tursa 2017년 7월 2일
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
James Tursa
James Tursa 2017년 7월 3일
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 2017년 7월 3일
Thanks! Now it is really clear

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 7월 2일
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))

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by