Help with the matrix creation pls ? if possible not a hardcoded solution but a function .
Mostra commenti meno recenti
Hi i need some help, i need to create a matrix that will have n columns (n is the only input) and the output would be a matrix of n x m in a way that every row has a sum of 1, where all the numbers are positive and have an increment of 0.1. For example i n = 3 the first couple of rows would look like this
0 0 1
0 0.1 0.9
0 0.2 0.8
....
1 0 0
2 Commenti
Steven Lord
il 15 Set 2015
Your example matrix does not satisfy your requirement "where all the numbers are positive" since it contains 0 values. I assume you meant "where all the numbers are nonnegative?"
Matej Stambuk
il 15 Set 2015
Risposta accettata
Più risposte (2)
Jos (10584)
il 15 Set 2015
% brute force
A = [] ;
for k=0:10
for j=0:10-k
A(end+1,:) = [k, j 10-k-j] ;
end
end
A = A ./ 10
1 Commento
This code generates permutations (with replacement) of the vector 0:0.1:1, and then selects only the rows that sum to one. I don't claim that this is an efficient use of memory, but it works. For the most efficient code see my other answer.
N = 3;
V = 0:0.1:1;
[Y{N:-1:1}] = ndgrid(1:numel(V));
X = reshape(cat(N+1,Y{:}),[],N);
B = V(X);
Z = sum(B,2);
B = B(0.99<Z&Z<1.01,:);
And the output matrix for N=3 (N=4 is below):
>> B
B =
0.00000 0.00000 1.00000
0.00000 0.10000 0.90000
0.00000 0.20000 0.80000
0.00000 0.30000 0.70000
0.00000 0.40000 0.60000
0.00000 0.50000 0.50000
0.00000 0.60000 0.40000
0.00000 0.70000 0.30000
0.00000 0.80000 0.20000
0.00000 0.90000 0.10000
0.00000 1.00000 0.00000
0.10000 0.00000 0.90000
0.10000 0.10000 0.80000
0.10000 0.20000 0.70000
0.10000 0.30000 0.60000
0.10000 0.40000 0.50000
0.10000 0.50000 0.40000
0.10000 0.60000 0.30000
0.10000 0.70000 0.20000
0.10000 0.80000 0.10000
0.10000 0.90000 0.00000
0.20000 0.00000 0.80000
0.20000 0.10000 0.70000
0.20000 0.20000 0.60000
0.20000 0.30000 0.50000
0.20000 0.40000 0.40000
0.20000 0.50000 0.30000
0.20000 0.60000 0.20000
0.20000 0.70000 0.10000
0.20000 0.80000 0.00000
0.30000 0.00000 0.70000
0.30000 0.10000 0.60000
0.30000 0.20000 0.50000
0.30000 0.30000 0.40000
0.30000 0.40000 0.30000
0.30000 0.50000 0.20000
0.30000 0.60000 0.10000
0.30000 0.70000 0.00000
0.40000 0.00000 0.60000
0.40000 0.10000 0.50000
0.40000 0.20000 0.40000
0.40000 0.30000 0.30000
0.40000 0.40000 0.20000
0.40000 0.50000 0.10000
0.40000 0.60000 0.00000
0.50000 0.00000 0.50000
0.50000 0.10000 0.40000
0.50000 0.20000 0.30000
0.50000 0.30000 0.20000
0.50000 0.40000 0.10000
0.50000 0.50000 0.00000
0.60000 0.00000 0.40000
0.60000 0.10000 0.30000
0.60000 0.20000 0.20000
0.60000 0.30000 0.10000
0.60000 0.40000 0.00000
0.70000 0.00000 0.30000
0.70000 0.10000 0.20000
0.70000 0.20000 0.10000
0.70000 0.30000 0.00000
0.80000 0.00000 0.20000
0.80000 0.10000 0.10000
0.80000 0.20000 0.00000
0.90000 0.00000 0.10000
0.90000 0.10000 0.00000
1.00000 0.00000 0.00000
And the output matrix for N=4:
>> B
B =
0.00000 0.00000 0.00000 1.00000
0.00000 0.00000 0.10000 0.90000
0.00000 0.00000 0.20000 0.80000
0.00000 0.00000 0.30000 0.70000
0.00000 0.00000 0.40000 0.60000
0.00000 0.00000 0.50000 0.50000
0.00000 0.00000 0.60000 0.40000
0.00000 0.00000 0.70000 0.30000
0.00000 0.00000 0.80000 0.20000
0.00000 0.00000 0.90000 0.10000
0.00000 0.00000 1.00000 0.00000
0.00000 0.10000 0.00000 0.90000
....
0.80000 0.00000 0.20000 0.00000
0.80000 0.10000 0.00000 0.10000
0.80000 0.10000 0.10000 0.00000
0.80000 0.20000 0.00000 0.00000
0.90000 0.00000 0.00000 0.10000
0.90000 0.00000 0.10000 0.00000
0.90000 0.10000 0.00000 0.00000
1.00000 0.00000 0.00000 0.00000
Categorie
Scopri di più su Logical 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!