24 views (last 30 days)

Hye!

I have to solve the following problem:

Consider a matrix M with only the numbers 1 to 9 as elements.

M =

2 9 3 2 4

8 6 4 8 5

5 7 1 6 4

9 8 9 5 1

Consider one of the elements M(i,j) that's not on the edge of the matrix. Such element always has 8 neighbours. If M(i,j) > 1 and each number from 1 to M(i,j)-1 is one of the 8 neighbours, we say that element is neighboring. If M(i,j) = 1, the element is automatically neighboring.

For example, M(2,2) is neighboring because 1,2,3,4 and 5 are one of the element's neighbours. M(3,4) on the other hand isn't neighboring because 2 and 3 don't occur around the element.

Now, I have to write a function that has 3 inputs: a matrix M and a row- and column index. The function has to control whether de element is neighboring or not and has a logical 0 or 1 as output.

Andrei Bobrov
on 14 Jan 2020

Edited: Andrei Bobrov
on 14 Jan 2020

function out = find_neighbor(M,i,j)

out = all(ismember(1:M(i,j)-1,M(i-1:i+1,j-1:j+1)));

end

Guillaume
on 14 Jan 2020

The if is not needed, out is true anyway, if M(i,j) is 1, since all([]) is true.

Opportunities for recent engineering grads.

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

Start Hunting!
## 9 Comments

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785942

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785942

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785945

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785945

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785947

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785947

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785949

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785949

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785958

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785958

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785979

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785979

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785992

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_785992

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_786004

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_786004

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_786005

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/500222-how-to-solve-the-following-exercise#comment_786005

Sign in to comment.