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에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
