How to replace multiple cells in an array with DIFFERENT random numbers
5 ビュー (過去 30 日間)
古いコメントを表示
Brigitta Rongstad
2017 年 3 月 20 日
コメント済み: Brigitta Rongstad
2017 年 3 月 20 日
I'm attempting to replace values above a certain threshold in array with different random numbers. My code currently replaces all of these values above the threshold with the same random number. I can do this really easily in Excel, but can't seem to find the right code in matlab. I know a loop is probably the best approach, but none of my attempts have worked yet.
Here's an example of my current code
muo_t2 = mean(ERDC128Bx_obliq);
stdo_t2 = std(ERDC128Bx_obliq)/(mean(ERDC92Bx_obliq) - muo_t2);
obliq_t2 = obliq;
obliq_t2( obliq_t2 > muo_t2 ) = norminv(rand(),muo_t2,stdo_t2);
0 件のコメント
採用された回答
Steven Lord
2017 年 3 月 20 日
Calling rand() generates one random number. When you try to assign that one random number into a region of another array that is larger than one element, MATLAB performs expansion. Try calling rand with a size vector to generate one element for each element of the array that you're trying to fill.
A = magic(3);
% (A > 5) has four true values, so we need 4 random values to fill those locations
A(A > 5) = rand(1, 4)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!