Find the largest number adjacent to a 0 in a vector

2 ビュー (過去 30 日間)
Janell Lopez
Janell Lopez 2016 年 4 月 21 日
コメント済み: Guillaume 2016 年 4 月 21 日
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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by