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.

댓글 수: 9

darova
darova 2020년 1월 14일
Please show your attempts
Ellen De Jonghe
Ellen De Jonghe 2020년 1월 14일
That's the problem, I ain't got one. I just don't see it :).
darova
darova 2020년 1월 14일
I don't want to do all your work for you. No one is gonna pay me for this
Bob Thompson
Bob Thompson 2020년 1월 14일
What are the steps that you think you need to accomplish conceptually?
How familiar are you with MATLAB?
Guillaume
Guillaume 2020년 1월 14일
That's the problem, I ain't got one. I just don't see it :).
First step: write down the algorithm?
  • How would you list the 8 neighbours
  • Once you've listed the neighbours, how would you check that they make up the set from 1 to M(i, j)?
Depending on which functions you're allowed to use, the whole thing can be done in just one line, so it's not a very hard problem.
Thanks for your logical insight. I'm always surprised how simple things are. But I always seem to make problems more complicated, especially when using MATLAB (btw: I'm only using it for a couple of weeks right now).
I made this script. It works, but I'm sure it can be done easier, especially the part where I create the array with the neighbours (n).
function [res] = isNeighboring(matrix,row,column)
n = zeros(1,8);
n(1,1) = matrix(row+1,column+1);
n(1,2) = matrix(row+1,column);
n(1,3) = matrix(row+1, column-1);
n(1,4) = matrix(row, column-1);
n(1,5) = matrix(row, column+1);
n(1,6) = matrix(row-1, column+1);
n(1,7) = matrix(row-1, column);
n(1,8) = matrix(row-1, column-1);
m = 1:matrix(row,column)-1;
p = ismember(m,n);
res = sum(p) == length(m);
end
Bob Thompson
Bob Thompson 2020년 1월 14일
That's definitely a really good start. I have a couple of thoughts to add.
1) How does your code respond to the selected element being 1? I think you may have a problem there.
2) You can always just take the entire 3x3 around the selected element. Even if it contains the element that shouldn't be a problem with your logic check.
n = matrix(row-1:row+1,column-1:column+1);
@Ellen, you've got the correct algorithm. As you suspec and Bob pointed out, the construction of n can be done in just one line with simple indexing.
@Bob, no the code also works for 1. m would be empty, so ismember will return empty. sum(empty) is 0 which is also the length of empty.
For the record, the one-liner I was talking about is
res = all(ismember(1:matrix(row, col)-1, matrix(row-1:row+1, col-1:col+1)))
Ellen De Jonghe
Ellen De Jonghe 2020년 1월 14일
Thanks!
I indeed forgot to check the case where M(i,j) = 1, I will try to figure that out.

댓글을 달려면 로그인하십시오.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2020년 1월 14일
편집: Andrei Bobrov 2020년 1월 14일

1 개 추천

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

댓글 수: 2

Guillaume
Guillaume 2020년 1월 14일
The if is not needed, out is true anyway, if M(i,j) is 1, since all([]) is true.
Andrei Bobrov
Andrei Bobrov 2020년 1월 14일
Thanks Guillaume! I'm fixed.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ellen De Jonghe
Ellen De Jonghe 2020년 1월 14일

0 개 추천

That's a very easy one! Thanks.
I seem to always make my scripts much longer than it has to.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2020년 1월 14일

편집:

2020년 1월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by