How to do permutations on a triangular matrix so that every permuted element goes from a filled to a non-filled cell?

5 Ansichten (letzte 30 Tage)
Hi, all. I want to create a shuffled matrix that comes from a triangular matrix. The point is to permute elements from the initially filled points (under the main diagonal) to the initially empty points (above the diagonal).
The tricky part is that I need it to randomly select points (say 10, 25 or 50% of the total filled points) to be permuted and that the number of empty and filled points in initial and final matrices shall be the same.
To make things a bit more interesting, on the top of all that, the main diagonal shall be made of zeros only.
Any suggestions?

Akzeptierte Antwort

Igor de Britto
Igor de Britto am 8 Mär. 2012
T = tril(X) The point was to create an index of the initially empty and initially filled cells. It may not be the most elegant solution, but a few for loops did work.
initiallyFilled = [];
i = 0;
for collumns = 1:X
for lines = 1:X
i = i+1 % this gives the index of the cell being evaluated
if lines > collumns
initiallyFilled = [initiallyFilled i]; % Agregates a filled cell index.
end
end
end
Then it's created a similar pattern for the initially empty cells. Difference being that lines < collumns on the final "if" statement. initiallyEmpty = []; i = 0;
for collumns = 1:X
for lines = 1:Y
i = i+1 % this gives the index of the cell being evaluated
if lines < collumns
initiallyEmpty = [initiallyEmpty i]; % Agregates an empty cell index.
end
end
end
In the end, it's a matter of choosing how many permutations will happens. For instance, I was doing some percents of the total, so:
numberOfPermutations = sum(T);
After that, it's just about randomizing wich index are going to be carried from one side to the other and, then, deleting the used index from the list of possibilities.
for a = 1:numberOfPermutations
removedFilled = ceil(rand() * lenght(inittialyFilled));
filledEmpty = ceil(rand() * lenght(inittialyEmpty));
T(initiallyFilled(removedFilled)) = 0;
T(inittialyEmpty(filledEmpty)) = 1;
initiallyFilled(removedFilled) = [];
inittialyEmpty(filledEmpty) = [];
end

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by