Randomly generated row matrix B with constraints?

조회 수: 2 (최근 30일)
laurie
laurie 2015년 2월 25일
답변: Roger Stafford 2015년 2월 25일
Matrix A which is generated based on a calculation A=[2 1 1 2]
I need:
Matrix B (B is equal to the size of A) needs to be randomly generated so that each of its entries (1,i) are no larger than the respective entries in Matrix A (1,i).
I also need a 0 to be a possible randomly generated number in Matrix B
Any help, much appreciated. I can not figure a way for randi to apply this constraint to the individual columns.
  댓글 수: 1
laurie
laurie 2015년 2월 25일
for pop = 2:1:20
for ii = 1:1:4
A(ii) = ((Data.tend(1,pop-1)-Data.duration(1,pop-1)+1) - Data.tstart(1,pop-1)); %Matrix A
B(ii) = randi([0, A(ii)], 1); %Matrix B
chrom(ii) = Population(pop-1).chrom(pop-1) + B(ii);
end
Population(pop).chrom = int2str(chrom);
clear chrom
end
Does not stay within upper and lower bounds after 1st iteration.

댓글을 달려면 로그인하십시오.

답변 (3개)

Roger Stafford
Roger Stafford 2015년 2월 25일
I don't understand what you mean in the sentence "I also need a 0 to be a possible randomly generated number in Matrix B". For the other part try this:
B = A.*rand(size(A));
For each element in A, the corresponding element in B will lie somewhere between 0 and the A element value.
If you expect the elements of B to be integers, that is quite another matter.
  댓글 수: 3
Roger Stafford
Roger Stafford 2015년 2월 25일
Are you saying that the values in B should be integers? You should make that clear. The code I gave does not do that. Its values are almost all fractional. Also please be clear about what you expect the lower bounds in B values to be. Is it zero? Is it one? And finally what did you mean by the sentence I asked about?
laurie
laurie 2015년 2월 25일
편집: laurie 2015년 2월 25일
1.The values should be integers
2. The lower bounds of B should be 0 and the upper bound for B(1,i) should be A(1,i); where i is the columns.
3.As for the sentence you asked about, I was referring to the lower bounds. I was not very clear, sorry about that.

댓글을 달려면 로그인하십시오.


Greig
Greig 2015년 2월 25일
I guess this is what you are looking for?
for ii = 1:length(A)
B(ii) = randi([0, A(ii)], 1);
end
It is not so clear exactly what you are after, could you clarify a bit more please.

Roger Stafford
Roger Stafford 2015년 2월 25일
I think this is what you want, Laurie, assuming A elements are all non-negative integers.
B = floor((A+1).*rand(size(A)));

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by