Help with the matrix creation pls ? if possible not a hardcoded solution but a function .

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 comentarios

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?"
You are right, my bad. should be nonnegative

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 15 de Sept. de 2015
Editada: Stephen23 el 15 de Sept. de 2015
This is Roger Stafford's memory-efficient solution, proposed here http://de.mathworks.com/matlabcentral/newsreader/view_thread/143037. It runs about twice as fast a Jos' and my other solutions.
n = 3;
d = 10;
c = nchoosek(1:d+n-1,n-1);
m = size(c,1);
t = ones(m,d+n-1);
t(repmat((1:m).',1,n-1)+(c-1)*m) = 0;
u = [zeros(1,m);t.';zeros(1,m)];
v = cumsum(u,1);
x = diff(reshape(v(u==0),n+1,m),1).'/d;
Where the output array is:
>> x
x =
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

1 comentario

this is great, for a moment i thought ill have to run a program on external server :D Thank you

Iniciar sesión para comentar.

Más respuestas (2)

% 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 comentario

Stephen23
Stephen23 el 15 de Sept. de 2015
Editada: Stephen23 el 15 de Sept. de 2015
This works for n=3, but how does this generalize to other n?

Iniciar sesión para comentar.

Stephen23
Stephen23 el 15 de Sept. de 2015
Editada: Stephen23 el 15 de Sept. de 2015
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

Categorías

Etiquetas

Preguntada:

el 14 de Sept. de 2015

Comentada:

el 15 de Sept. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by