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

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

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2015년 9월 15일
편집: Stephen23 2015년 9월 15일
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

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

댓글을 달려면 로그인하십시오.

추가 답변 (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

This works for n=3, but how does this generalize to other n?

댓글을 달려면 로그인하십시오.

Stephen23
Stephen23 2015년 9월 15일
편집: Stephen23 2015년 9월 15일
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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2015년 9월 14일

댓글:

2015년 9월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by