manipulation d'une matrice
Mostrar comentarios más antiguos

bonjour , si j'ai une matrice M(4,6) d'entier affecté aléatoirement.La somme des entiers de chaque ligne de la matrice soit egual a 2000. je voulais distribuer aléatoirement les valeurs selon des pourcentage données dans le fichier word ci-jointe. Merci.
Respuesta aceptada
Más respuestas (1)
loukil sana
el 15 de Mzo. de 2016
0 votos
2 comentarios
Ced
el 15 de Mzo. de 2016
No problem. Let me know if anything is unclear language-wise.
That is what the code above is doing, no? In the end, the total sum of each complete row is 2000.
Let's take the first row as an example. You need 60% from red and 40% from blue. 60% from 2000 is SRM11 = 1200, the remaining 40% are SRM12 = 800.
If your percentages change, you just change the starting sums. You could write a little function for that, e.g.
function [S1, S2] = split_sum(total_sum,p1,p2)
S1 = round(p1);
S2 = total_sum - S2;
end
One thing I forgot to mention: randi will only work if the percentages are greater than 0.005. If that is not guaranteed, then you need to either introduce an if statement to catch that case, or you can also use rand to construct an integer yourself.
loukil sana
el 15 de Mzo. de 2016
Categorías
Más información sobre Startup and Shutdown 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!
