manipulation d'une matrice
Show older comments

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.
Accepted Answer
More Answers (1)
loukil sana
on 15 Mar 2016
0 votes
2 Comments
Ced
on 15 Mar 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
on 15 Mar 2016
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!