How to create matrix with fixed sum in rows and fixed increment in elements
Show older comments
I would like to create a 231*3 matrix whose elements are all multiples of 0.05 and the sum of each row be equal to 1.
E.g.
0 0 1
0 0.05 0.95
0.05 0 0.95
0 0.1 0.90
0.1 0 0.90
0.05 0.05 0.90
......
Accepted Answer
More Answers (1)
Bruno Luong
on 7 Sep 2018
Edited: Bruno Luong
on 7 Sep 2018
s = 0.05;
n = round(1/s);
m = 3;
r = nchoosek(1:n+m-1,m-1);
z = zeros(size(r,1),1);
r = (diff([z, r, n+m+z],1,2)-1)/n
4 Comments
Rik
on 7 Sep 2018
I don't know if it is relevant, but this method is not guaranteed to give unique rows.
Bruno Luong
on 7 Sep 2018
I miss understood whereas OP wants random or all combinations. Now I fix it since the number of combinations is just 231.
Zijin Xu
on 7 Sep 2018
Stephen23
on 7 Sep 2018
+1 Very nice.
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!