Find all and modify triplicates in an array

3 views (last 30 days)
I have a 10x10 array of ones and zeros. I need to come up with a script that will search the array for any cases where a row, column, or diagonal in the array contains a triplicate (three of a kind) of either ones or zeros and replace the third number with the opposite value. For example, if row 1 is 0011100101 then because the 3rd, 4th, and 5th columns are all equal to one and as I stated previously, there can be no three of a kinds in the array, column 5 in the first row becomes 0. This then makes column 7 need to become 1 because we end up with another triplicate, and then we're done with that row. A similar process occurs with the search in the columns and diagonals. Is my code as simple as a nested for loop that says "if your current row and column equals the next column over and the next column over then the third column gets changed" and similar for the checking the columns and diagonals? Can't seem to figure this one out.
  6 Comments
Morgan Clendennin
Morgan Clendennin on 27 Aug 2018
Any time a triplet appears, one of the values needs to change. In the first case where the triplets are purely horizontal, the array would go from 11100 to 11001. In the second case, I guess I didn't see the problem when two opposite triplets intersect at a row and column intersection point. In this case, we could change the column to 110 but we would have to change one of the other places in the horizontal triplet (leading to either 010 or 100) as changing it to 001 would change back the vertical triplet. In keeping with changing the third (last) value of the triplet, I believe in the second case it should become
1
1
1 0 0
Hopefully this makes sense.
Stephen23
Stephen23 on 27 Aug 2018
Edited: Stephen23 on 28 Aug 2018
@Morgan Clendennin: have you confirmed if these rules lead to a stable solution? It seems quite possible that you could end up with a kind of Game of Life situation.

Sign in to comment.

Answers (2)

Malte Herrmann
Malte Herrmann on 24 Aug 2018
Edited: Malte Herrmann on 24 Aug 2018
Here you go!
function [myArray, NChanges] = ChangeTripletsInArray(myArray)
NChanges = 0;
% Iterate over rows
for idxr = 1:size(myArray, 1)
% Iterate over columns
for idxc = 1:size(myArray, 2)-2
threevals = myArray(idxr, idxc:idxc+2); % Take 3 values from array
[threevals_new, flag, NChanges] = CheckTrips(threevals, NChanges);
if flag == 1 % Then changes have to be included in myArray
myArray(idxr, idxc:idxc+2) = threevals_new;
end % End of if flag == 1
end % End of for idxc
end % End of for idxr
fprintf('%1.0f changes had to be made to the array.')
%%%Subfunction CheckTrips
function [self, flag, n] = CheckTrips(self, n)
if mod(sum(self), 3) == 0 % e.g. sum([1,1,1])=3 mod(3,3)=0
% sum([0,0,0])=0 mod(0,3)=0
self(end) = abs(self(end)-1); % e.g. abs(1-1) = 0
% abs(0-1) = 1
flag = 1;
n = n + 1;
else
flag = 0;
end
end % End of function CheckTrips
end % End of function ChangeTripletsInArray
  6 Comments
Morgan Clendennin
Morgan Clendennin on 28 Aug 2018
Just me again, I realized kinda by accident that if you take the output of your ChangeTripletsInArray function and transpose it and then put it back in to the function, that takes care of the columns as if they were rows. This is again testing this all with a 10x10 ones matrix as my test case. This now creates the same problem that my code has where if we change the diagonal triplets by the third value, we would create new triplets again on the rows/columns. Going to test it if we change the first member of the diagonal triplets. I'm getting closer!!
Morgan Clendennin
Morgan Clendennin on 11 Sep 2018
Hi Malte, have you been able to look more into this?

Sign in to comment.


Stephen23
Stephen23 on 27 Aug 2018
This is a simple example of how you could approach this problem. I have not dealt with the edge cases: this is a simple proof-of-concept demonstration which you can tailor as required. Note that your rules do not seem to guarantee any fixed solution in a finite number of iterations.
I = logical(randi(0:1,10));
imh = image(I,'CDataMapping','scaled');
while true
idx = I(1:8,:)&I(2:9,:)&I(3:10,:); % rows
I(idx) = ~I(idx);
set(imh,'CData',I)
pause(0.5)
idx = I(:,1:8)&I(:,2:9)&I(:,3:10); % columns
I(idx) = ~I(idx);
set(imh,'CData',I)
pause(0.5)
idx = I(1:8,1:8)&I(2:9,2:9)&I(3:10,3:10); % diag
I(idx) = ~I(idx);
set(imh,'CData',I)
pause(0.5)
idx = I(1:8,3:10)&I(2:9,2:9)&I(3:10,1:8); % antidiag
I(idx) = ~I(idx);
set(imh,'CData',I)
pause(0.5)
end
  3 Comments
Stephen23
Stephen23 on 28 Aug 2018
Edited: Stephen23 on 28 Aug 2018
" Your provided I array has a few different solutions that it shows but it infinitely loops as a correct solution where the three conditions is never met."
So far your rules do not guarantee that there is any solution. While there might be alternative ways to approach this (e.g. .using a some kind of global optimization routine to find a minimum energy state, which may or my not have triples), so far there is nothing in your rules that prevents conflict situations oscillating indefinitely, if you use a simple iterative method.
Morgan Clendennin
Morgan Clendennin on 28 Aug 2018
How would you do it, if not an iterative method? Also, what if we were to change the location of the replacement in the triplet to be randomized? i.e. 111 could be replaced with either 110, 101, or 011.

Sign in to comment.

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!