Randomly generated row matrix B with constraints?
Mostrar comentarios más antiguos
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 comentario
laurie
el 25 de Feb. de 2015
Respuestas (3)
Roger Stafford
el 25 de Feb. de 2015
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 comentarios
laurie
el 25 de Feb. de 2015
Roger Stafford
el 25 de Feb. de 2015
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?
Roger Stafford
el 25 de Feb. de 2015
I think this is what you want, Laurie, assuming A elements are all non-negative integers.
B = floor((A+1).*rand(size(A)));
Categorías
Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!