Find all and modify triplicates in an array
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 27 Aug. 2018
Bearbeitet: Stephen23
am 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.
Antworten (2)
Malte Herrmann
am 24 Aug. 2018
Bearbeitet: Malte Herrmann
am 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 Kommentare
Stephen23
am 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 Kommentare
Stephen23
am 28 Aug. 2018
Bearbeitet: Stephen23
am 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.
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!