Find the largest number adjacent to a 0 in a vector

1 次查看(过去 30 天)
I must write a function that returns the largest number adjacent to a 0 in a given vector of positive or negative numbers (doubles). I am not allowed to use imdilate, diff, or unique. This is what I have so far:
function [ bignum ] = largest( vec )
%find zero
%store surrounding elements in vector
%max
zs=find(~vec);
n=length(zs);
newvec=[];
if zs(1)==1
newvec=[newvec vec(zs(1)+1)];
end
if zs(end)==length(vec)
newvec=[newvec vec(zs(end)-1)];
end
for i=1:n
if zs(i)~=1 && zs(end)~=n
newvec=[newvec vec(zs(i)-1) vec(zs(i)+1)];
end
end
bignum=max(newvec);
end
If I take out all of the if statements in my code, it works, but not if the 0s are at the end or beginning of the vector (I know it's because of my indices). I put in the if statements to try to account for those cases, but it still gives me an index error. I don't know why. Should I scrap this whole idea for a new way of thinking?

回答(3 个)

Guillaume
Guillaume 2016-4-21
I would simply find the positions of all the zeros, add +1 and -1 to these positions and concatenate into a vector, use that vector to index the original array, and apply max on that:
zeropos = find(~vec);
zeroadj = [zeropos(:) - 1; zeropos(:) + 1]; %the colons so that it works with row or column vectors
zeroadj(zeroadj < 1 | zeroadj > numel(vec)) = []; %remove invalid indices
bignum = max(vec(zeroadj));
  2 个评论
Walter Roberson
Walter Roberson 2016-4-21
Not quite. If your data was all negative other than the zeros, but you had two zeros in a row, then your code would pull out some zeros and would max() those zeros in with the negative numbers, giving 0 as the result.
Guillaume
Guillaume 2016-4-21
True, this would be solved by setdiff'ing zeroadj with zeropos, or just working with logicals:
zeropos = ~vec(:);
zeroadj = ~zeropos & ([false; zeropos(2:end)] | [zeropos(1:end-1); false)]);
bignum = max(vec(zeroadj));

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2016-4-21
Here is a useful trick: strfind() can be used on logical vectors. For example,
data = rand(1,50) > 0.9;
strfind(data, [0 1])
"find me the places where a condition is true right next to a place the condition is false"

Image Analyst
Image Analyst 2016-4-21
Here's a fairly straightforward step-by-step way that I think you can follow:
data = rand(1, 50); % Create some random numbers.
% Make some zeros in it
randomIndexes = randperm(length(data), 20);
data(randomIndexes) = 0
% Find elements to the right of zeros.
leadingEdges = strfind(data~=0, [0, 1])+1
% Find elements to the left of zeros.
trailingEdges = strfind(data~=0, [1, 0])
% Pull out all elements next to a 0.
indexesToExtract = sort([leadingEdges, trailingEdges])
edgeElements = data(indexesToExtract)
% Find the biggest one
maxNextToZero = max(edgeElements)
You could make it more compact of course. I just stretched it out so you can digest it easier in smaller chunks.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by