How do I create a matrix with the diagonals set to 10 and non-diagonals a random integer (0 to 2) where each row sums to 10 (without including diagonal)?
Mostra commenti meno recenti
I need to create a 15x15 matrix where the diagonals are all 10 and the numbers off the diagonal are a random integer between 0 and 2. I also need that non-diagonal numbers to sum to 10 for each row. See the below matrix, first row for an example.
M =
10 1 0 0 2 0 2 1 1 0 2 0 0 1 0
0 10 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 10 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 10 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 10 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 10 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 10 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 10 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 10 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 10 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 10 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 10 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 10 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 10 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 10
Risposta accettata
Più risposte (2)
John D'Errico
il 2 Feb 2016
Simple.
First, generate all possible partitions of the number 10, as a sum of the integer [0,1,2]. I'll use my partitions function to see how to do it.
partitions(10,[0 1 2],[],10)
ans =
0 10 0
1 8 1
2 6 2
3 4 3
4 2 4
5 0 5
Thus, we can write 10 as
10 ones
8 ones and a 2
6 ones, and a pair of twos
4 ones and 3 twos
2 ones and 4 twos
5 twos
Your matrix has 9 columns in it, if we exclude the diagonal, so we can exclude the case where we have 10 ones.
Once you have generated all possible rows, just select randomly from that set to fill each row.
1 Commento
the cyclist
il 2 Feb 2016
Modificato: the cyclist
il 2 Feb 2016
John, that syntax of partitions() gives the error
Error using partitions (line 143)
All members of candidate_set must be > 0
the cyclist
il 2 Feb 2016
matSize = 10;
% Partitions of matSize; Use's John D'Errico's utility from FEX
p = partitions(matSize);
% Exclude those that have values 3-10 in them.
p = p(sum(p(:,3:end),2)==0,:);
% Exclude the partition that is ten 1's, because there are only 9 spots to fill
p(1,:) = [];
% Remove the spurious columns
p(:,all(p==0)) = [];
% Append the zero counts
p = [matSize-1 - sum(p,2), p];
numberRows = size(p,1);
% For each partition, construct all the permutations
allPermutations = [];
for nr = 1:numberRows
basis = [zeros(1,p(nr,1)) ones(1,p(nr,2)) 2*ones(1,p(nr,3))];
thesePermutations = unique(perms(basis),'rows');
allPermutations = [allPermutations; thesePermutations];
end
numberAllPermutations = size(allPermutations,1);
M = matSize * eye(matSize);
for nr = 1:matSize,
rowRand = allPermutations(randi(numberAllPermutations),:);
M(nr,1:nr-1) = rowRand(1:nr-1);
M(nr,nr+1:end) = rowRand(nr:end);
end
2 Commenti
the cyclist
il 2 Feb 2016
Modificato: the cyclist
il 2 Feb 2016
This is vastly slower than Kirby's solutions, probably because I am lazily generating many more partitions than required. Combining John's own more efficient use of partitions() in his answer would be much better. Unfortunately, I could not get his syntax to work.
the cyclist
il 2 Feb 2016
Oops. Nope. I just profiled my code. The slowness is NOT the partitioning, even though I am using it dumbly. The slow part is generating the permutations.
Categorie
Scopri di più su Resizing and Reshaping Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!