Find all and modify triplicates in an array
Show older comments
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
jonas
on 24 Aug 2018
I feel that the problem is still ambiguous. Do you search for triplets from left to right? Do you always start with ones? Can you have more than three of a kind in a row?
Image Analyst
on 24 Aug 2018
What's the use case for this? Why do you need to do it (provide context for this unusual thing)? Do they have to be integers? Can you just set every other one in a run of 3 or longer to 0?
Morgan Clendennin
on 24 Aug 2018
Stephen23
on 27 Aug 2018
@Morgan Clendennin: how do you want to resolve conflicts? For example, a sequence like this:
1 1 1 0 0
When the third element is changed from 1 -> 0 it creates a new sequence of three 0's: does the third 0 need to be changed? What about:
1
1
0 0 1
If we process the columns and change the third 1 -> 0, what about the three 0's in row?
Morgan Clendennin
on 27 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.
Answers (2)
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
on 24 Aug 2018
Morgan Clendennin
on 25 Aug 2018
Malte Herrmann
on 27 Aug 2018
Hi there! I obviously was reading too fast, had only the rows in mind. Sorry for the confusion! I'm quite busy this week, so I don't think I'll be able to sit on this for a longer time, but it seems to be a real brain teaser! Hopefully you'll be able to figure it out, extending this to vertical and diagonal surely will be quite difficult, especially if the code is supposed to work efficiently. But in a first try, looping until all conditions are fulfilled sounds like it will lead to a good solution!
Morgan Clendennin
on 27 Aug 2018
Morgan Clendennin
on 28 Aug 2018
Morgan Clendennin
on 11 Sep 2018
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
Morgan Clendennin
on 27 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
on 28 Aug 2018
Categories
Find more on Creating and Concatenating 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!