How to replace multiple cells in an array with DIFFERENT random numbers
5 次查看(过去 30 天)
显示 更早的评论
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!