Generate matrix of precise length and in ascending order

13 次查看(过去 30 天)
Hello, i want to generate a matrix of elements like the following :
N = 10; M = 4; N and M are variables.
A = [1 1 1 7,
1 1 2 6,
1 1 3 5,
1 1 4 4.....]
so on and so forth.
Basically the first element should be one and rest of the elements should be in the ascending order. Also the matrix has to satisfy the row sum of N and the number of elements as specified by M.
I have used randperm, but i cannot maintain the specifed order.
Thanks for your replies.
  3 个评论
Sai Kumar
Sai Kumar 2020-2-3
编辑:Sai Kumar 2020-2-3
Thanks for your reply.
I have used sort, and it sorts row wise perfectly satisfying the sum.
this is my code :
for i = 1:10
v{i} = sort(diff([0,sort(randperm(N-1,M-1)),M]));
A = cell2mat(v);
end
Z = reshape(A,[],M).';
but with this, i wont get the above pattern which i mentioned in my question.
J. Alex Lee
J. Alex Lee 2020-2-3
I am not sure that your posted code is accomplishing your stated objective...
I'm also just seeing a pattern that I didn't see before in your example. The 3rd column has ascending values...is that a feature you want, or an accident of your example? I think your mention of randperm suggested that you want a random matrix with the following features:
  1. first column is always 1's
  2. row sums are always N
  3. Number of columns is fixed to M
How about number of rows? And then all of David's questions below.

请先登录,再进行评论。

回答(3 个)

David Hill
David Hill 2020-2-3
What are your numbers limited to? (1-10)? Do you need all combinations? If numbers limited to 1-R and you only need some random combinations:
R=10;%numbers in maxtrix limited to 1-10
A=randi(10,[10000,M]);%the 10000 can be whatever you want but enough to generate several row sums == N
a=sum(A,2);
A=A(a==N,:);
A=sort(A,2);
A=sortrows(A,1:M);
A=unique(A,'rows');
  4 个评论
David Hill
David Hill 2020-2-3
Yes, but you could easily just delete all rows not starting in 1. I really don't know exactly what is wanted.
Sai Kumar
Sai Kumar 2020-2-3
thanks for your reply.
The number R is not limited to 10, but chagelable. But as pointed out, i need to have different combination of numbers inside the matrix which satifies the sum and also the number of elements inside the matrix.
Your code works.
Thanks for help.

请先登录,再进行评论。


J. Alex Lee
J. Alex Lee 2020-2-3
This seems to work, and is less wasteful
RowSum = 10;
NCols = 4;
NRows = 70000;
A = randi(floor((RowSum-2)/(NCols-2)),[NRows,NCols-2]);
Remainder = RowSum - sum(A,2) - 1;
A = sort([ones(NRows,1),A,Remainder],2);
% test
any(RowSum~=sum(A,2))
any(A(:,1)~=1)

Sai Kumar
Sai Kumar 2020-2-3
ok guys, i have run multiple cases with both your code and seems like i am failing in some cases.
I have searched on matlab central and found on the following link.
The above program gives all possible outcomes starting with 1, repetitive and also combinations.
I was able to modify my code and get the out come like this :
say suppose i have N = 7, M = 3;
my output matrix will be [1 1 5;
1 2 4;
1 3 3;
2 3 3];
Thanks for your support.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by