Filter löschen
Filter löschen

How to replace multiple cells in an array with DIFFERENT random numbers

5 Ansichten (letzte 30 Tage)
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);

Akzeptierte Antwort

Steven Lord
Steven Lord am 20 Mär. 2017
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)

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by