How to obtain a Matrix?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi evreyone, I am trying to obtain a matrix [n,n] by a vector [n,1] in order that the total (sum) of each row must be equal to the vector's row. See the example
vector=[ 5; 10; 3] matrix=[1,2,1,1; 2,3,4,1; 3,0,0]
There is any funciont in matlab that do this? I need that the values in the matrix are not all equal.
it's okay if it gives (2,1,2) but if it does (2,2,2) it isn't what i am looking for. It would be even better if the values in the matrix changes evrey time I run the code. For example: [1 run]-> 5= [1,1,2,0,1] | [2 run]-> 5= [1,2,2,0,0] | [3 run]-> 5= [1,3,0,0,1] and so on
Hope you can help me
댓글 수: 0
답변 (4개)
Matt J
2021년 6월 30일
편집: Matt J
2021년 6월 30일
Perhaps as follows?
vector=[ 5; 10; 3] ;
n=numel(vector);
matrix=zeros(n); %pre-allocate
s=vector;
for i=1:n
if i==n
col=s;
else
col=floor(rand*s);
end
matrix(:,i)=col;
s=max(s-col,0);
end
matrix,
check=all(sum(matrix,2)==vector)
댓글 수: 1
Matt J
2021년 7월 1일
편집: Matt J
2021년 7월 1일
Matt's code with a large vector returns some numbers in the first columns, in the central columns a series of zeros and in the last column of the matrix a series of 1s.
Here is a modification that distributes the values a bit more evenly and randomly across the rows (note the attached file).
n=15;
vector=randi(3*n,n,1);
matrix=zeros(n); %pre-allocate
s=vector;
for i=1:n
if i==n
col=s;
else
factor=rand(size(s))./(sqrt(n-i));
col=floor(factor.*s);
end
matrix(:,i)=col;
s=max(s-col,0);
end
[~,perm]=sortlidx(rand(size(matrix)),2);
matrix=matrix(perm),
check=all(sum(matrix,2)==vector)
Image Analyst
2021년 7월 1일
I don't believe it's a common enough thing to want to do that there is a built in function for it so you'll have to build it yourself.
If you're not restricting it to integers, this works:
vector = [5; 10; 3] % Column vector
matrix = repmat(vector', [length(vector), 1])
percentages = [0.5, 0.3, 0.2] % Fractions of number to appear in each row.
matrix(1,:) = matrix(1,:) * percentages(1);
matrix(2,:) = matrix(2,:) * percentages(2);
matrix(3,:) = matrix(3,:) * percentages(3)
matrix =
2.5 5 1.5
1.5 3 0.9
1 2 0.6
댓글 수: 0
Andrea Miceli
2021년 7월 1일
댓글 수: 4
Image Analyst
2021년 7월 1일
No offense taken - just trying to figure out what you want and if either what Matt or I did was what you want or need? From your explanation it sounds like floating point ratios of the numbers is fine - they don't need to be integers like Matt assumed (a logical assumption given your example). So now you can take continuous/floating point values but the question is how to pick the component numbers that sum up to your desired number. Of course, there is an infinite number of number sets that can sum up to the desired number if they don't need to be integers. So I did it two ways: (1) just use predetermined values (proportions), and (2) use random proportions. They both worked, but do you prefer one over the other? If you don't like either, then why not -- what are they lacking? And what method for picking the component numbers would you prefer instead? Do you have some formula for picking the proportions?
Walter Roberson
2021년 7월 5일
https://www.mathworks.com/matlabcentral/answers/327656-conditional-random-number-generation#answer_257296 has code.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!