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!