I would like to have a time domain function that uses a random number, but is stable over an interval. The anonymous function below does this, but it is not elegant.
f = @(t) {rng(ceil(t/10)), rand(1)}; % returns new random numbers each decade of t
f(2)
ans =
1×2 cell array
{1×1 struct} {[0.4170]}
The random number in second cell is what I want, but it can only be accessed through another variable. Is there a way in Matlab to simply return the random number without all the indirection?
Thank you for any help you can offer.

5 commentaires

William Buller
William Buller il y a environ 2 heures
Sometimes you just need to ask the question and then it comes to you!
f = @(t) isstruct(rng(ceil(t/10)))*rand(1);
Paul
Paul il y a environ 2 heures
Are there bounds on t?
Matt J
Matt J il y a environ 2 heures
f = @(t) isstruct(rng(ceil(t/10)))*rand(1);
Not a good idea. You are assuming sub-expressions in the function are evaluated left-to-right. That is not reliable.Why does the function have to be anonymous?
Walter Roberson
Walter Roberson il y a environ 2 heures
MATLAB has a well-defined order of operations, that mostly guarantees left-to-right operations (as modified by the operation precedences, that leads to oddities like c^a^b ). The main exception is the vaguely-documented linear algebra exceptions where for example a'*b might be specially evaluated rather than being evaluated as (a')*b
Matt J
Matt J il y a 25 minutes
@Walter Roberson The order of operations is left-to-right, but not the order of computation of the operands.

Connectez-vous pour commenter.

 Réponse acceptée

f = @(t) struct('rng', rng(ceil(t/10)), 'r1', rand(1)).r1
f = function_handle with value:
@(t)struct('rng',rng(ceil(t/10)),'r1',rand(1)).r1
f(2)
ans = 0.4170

1 commentaire

Matt J
Matt J il y a 22 minutes
Modifié(e) : Matt J il y a 15 minutes
I wouldn't trust this, at least not for posterity. Can we be sure struct() will always generate the fields in left to right order? Can we even be sure both struct fields will be assigned before the .r1 operation is applied?

Connectez-vous pour commenter.

Plus de réponses (4)

Matt J
Matt J il y a environ 2 heures
Modifié(e) : Matt J il y a environ 2 heures
function y=f(t)
rng(ceil(t/10));
y=rand(1);
end
Paul
Paul il y a environ 2 heures
t = 2*(rand(1,1000)-0.5)*100;
figure
plot(t,f(t,10),'b.',t,f(t,10),'r.')
function y = f(t,interval)
tinterval = ceil(t/interval);
[C,ia,ic] = unique(tinterval);
yout = rand(size(C));
y = yout(ic);
end
Matt J
Matt J il y a environ 2 heures
rng('default')
T= rand(1,10);
t=1:15;
T(ceil(t/10))
ans =
Columns 1 through 9
0.8147 0.8147 0.8147 0.8147 0.8147 0.8147 0.8147 0.8147 0.8147
Columns 10 through 15
0.8147 0.9058 0.9058 0.9058 0.9058 0.9058
Steven Lord
Steven Lord il y a environ une heure
If you can use a "named" function like Matt J's answer that would proably be cleanest.
But if you want to draw numbers from a random number generator with a particular seed value and they must be generated inside an anonymous function, without defining a "named" function, rng is not the right tool for this particular application. Use RandStream.create. This creates a temporary random number generator object from which you can draw numbers without affecting any other rand, randn, randi, etc. call.
f = @(t) rand(RandStream.create('twister', 'seed', t), 1, 2)
f = function_handle with value:
@(t)rand(RandStream.create('twister','seed',t),1,2)
f(1)
ans = 1×2
0.4170 0.7203
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
f(2)
ans = 1×2
0.4360 0.0259
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
f(1)
ans = 1×2
0.4170 0.7203
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rng(2, 'twister')
rand(1, 2) % Matches f(2)
ans = 1×2
0.4360 0.0259
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rng(1, 'twister')
f(3) % doesn't afect the global generator
ans = 1×2
0.5508 0.7081
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rand(1, 2) % Matches f(1)
ans = 1×2
0.4170 0.7203
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Catégories

Produits

Version

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by