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
Steven Lord
2015년 9월 15일
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
2015년 9월 15일
채택된 답변
추가 답변 (2개)
Jos (10584)
2015년 9월 15일
% 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 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에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!