필터 지우기
필터 지우기

Find the largest number adjacent to a 0 in a vector

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