manipulation d'une matrice
显示 更早的评论

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.
采纳的回答
更多回答(1 个)
loukil sana
2016-3-15
0 个投票
2 个评论
Ced
2016-3-15
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
2016-3-15
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
