manipulation d'une matrice
3 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
Ced
2016 年 3 月 15 日
編集済み: Ced
2016 年 3 月 15 日
I'm afraid your chances for an answer are probably quite slim if you pose the question in French. I will answer in English for everybody else's sake and hope you understand this, otherwise the code should be understandable.
Just in case... Si mon Anglais n'est pas clair, j'espère que le code en bas sera suffisant pour résoudre le problème.
Basically, if I understand correctly, you need to do two steps:
1. Get the total row sum of each color section according to your percentages so that the total sum of each row is 2000. This step is simple, I'm sure you can figure it out.
2. Distribute this sum among the members
For example, let's say the desired sum of the red section of RM1 is SRM11 = 1200. Therefore SRM12, being the blue part, requires SRM12 = 800.
One possible (and I think intuitive) way would be:
SRM11 = 1200; % Total sum for red part in first row (1ère ligne, rouge)
SRM12 = 800; % Total sum for blue part in first row (1ère ligne, bleu)
X = zeros(4,6);
X(1,1) = randi(SRM11,1); % get the first number
SRM11 = SRM11 - X(1,1); % see how much is left to distribute
X(1,2) = randi(SRM11,1); % assign another random integer
X(1,3) = SRM11 - X(1,2); % get the rest to ensure the sum is correct
X(1,4) = randi(SRM12,1); % repeat
SRM12 = SRM12 - X(1,4);
X(1,5) = randi(SRM12,1);
X(1,6) = SRM12 - X(1,5);
etc.
It would be more elegant to write this in a for loop and perform the operation on all sections simultaneously, but I think the above example is clearer in how things work, and I will leave the rest as a little exercise.
Hope this helps.
Cheers
0 件のコメント
その他の回答 (1 件)
loukil sana
2016 年 3 月 15 日
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.
参考
カテゴリ
Help Center および File Exchange で Transmitters and Receivers についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!