How do I write a function that checks whether there is a horizontal, vertical or diagonal connect N?
Show older comments
Input:
M = [0,0,0,2,0,0,0;0,0,0,2,0,1,0;0,2,0,1,1,2,0;0,1,2,1,1,1,0;1,2,1,2,2,2,0;2,2,1,2,2,1,2]
Visualization of input:
--------------- x = 1
| | | |o| | | | o = 2
| | | |o| |x| |
| | | |x|x|o| |
| |x|o|x|x|x| |
|x|o|x|o|o|o| |
|o|o|x|o|o|x|o|
---------------
A connect 4 is present:
0 0 0 2 0 0 0
0 0 0 2 0 1 0
0 0 0 1 1 2 0
0 1 2 1 1 1 0
1 2 1 2 2 2 0
2 2 1 2 2 1 2
My question:
How do I write a function that checks whether there is a horizontal, vertical or diagonal connect N (connect 4/5/6 etc. (program is scalable)) in this matrix?
9 Comments
Stephen23
on 16 Dec 2019
The Legend
on 16 Dec 2019
" the answers I received almost worked, but not entirely."
Based on your comment I updated my answer to work with all cases. So far the corrected code works with everything that you have shown, but you apparently have not tried it.
Is there are specific reason why you continue to ignore my edited answer?
"I really do appreciate the help but it didn't work."
In my earlier comment I asked you to provide any case that did not work with my edited answer. So far you have not provided any examples, or even replied to my comment.
The Legend
on 16 Dec 2019
"Your code does work, but it doesnt work for diagonals that go like this // for some reason"
Please show an example, together with the exact code that you are using.
It works:
>> F = @(x)conv2(x,eye(4),'same')>=4 | conv2(x,flipud(eye(4)),'same')>=4 | conv2(x,ones(4,1),'same')>=4 | conv2(x,ones(1,4),'sa
me')>=4;
>> M = [0,1,0,0,0;0,0,1,0,0;0,0,0,1,0;0,0,0,0,1;0,0,0,0,0]
M =
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
0 0 0 0 0
>> X1 = F(M==1);
>> any(X1(:))
ans = 1
>> M = [0,0,0,0,0;0,0,0,1,0;0,0,1,0,0;0,1,0,0,0;1,0,0,0,0]
M =
0 0 0 0 0
0 0 0 1 0
0 0 1 0 0
0 1 0 0 0
1 0 0 0 0
>> X1 = F(M==1);
>> any(X1(:))
ans = 1
Notice how I provide complete working examples, complete with the matrices and the function, which makes it easy for anyone reading this forum to check if they work or not.
Do you see a problem with my examples?
(notice also that the code you are using is NOT that from my updated answer).
I have no idea why you are asking me about someone else's code: if you have a problem with their answer, you should be asking them in a comment to their answer.
In any case, my edited answer detects both of those diagonals perfectly:
>> F = @(x)conv2(x,eye(4),'same')>=4 | conv2(x,flipud(eye(4)),'same')>=4 | conv2(x,ones(4,1),'same')>=4 | conv2(x,ones(1,4),'same')>=4;
>> M = [0,0,0,2,0,0,0;0,0,0,2,0,1,0;0,2,0,1,1,2,0;0,1,2,1,1,1,0;1,2,1,2,2,2,0;2,2,1,2,2,1,2]
M =
0 0 0 2 0 0 0
0 0 0 2 0 1 0
0 2 0 1 1 2 0
0 1 2 1 1 1 0
1 2 1 2 2 2 0
2 2 1 2 2 1 2
>> X1 = F(M==1);
>> X2 = F(M==2);
>> any(X1(:)) % from (2,6) to (5,2)
ans = 1
>> any(X2(:)) % from (3,2) to (6,5)
ans = 1
Once again, I will repeat my request: please provide one example which does not work using my edited answer. I look forward to your reply.
The Legend
on 16 Dec 2019
Edited: The Legend
on 16 Dec 2019
Stephen23
on 16 Dec 2019
"This function shouldnt return a 2 value, but a 0 value, idk why that's happning... there's no connect N."
Actually there is. Let me highlight it for you:
0 0 0 2 0 0 0
0 0 0 2 0 0 0
0 *2* 0 1 1 2 0
0 1 *2* 1 1 1 0
1 2 1 *2* 2 2 0
2 2 1 2 *2* 1 2
Why are you still using the outdated answer? Even though I have written around ten times that I updated/fixed my answer, you are still using the version that does not detect anti-diagonals.
The Legend
on 16 Dec 2019
Answers (0)
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!