Find the largest number adjacent to a 0 in a vector
조회 수: 2 (최근 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?
댓글 수: 0
답변 (3개)
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
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
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
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"
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!