error multiplying datetime (now) by 100 with *
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
the set up for a random number generator, to keep an accurate and repeatable timestamp of each participant in a psychophysics experiment
was coded as the following-
function baseSeed = SetUpRng
rng((sum(100)*(datetime("now"))));
baseSeed = floor(rand*2^32);
rng(baseSeed, 'twister');
the error message given was that '*' is not supported for operands of type 'datetime'
I looked up muliplication in MATLAB documentation, it had another option of 'times' but when I tried to use this it still gave an error message about checking for missing multiplication operator or unfinished delimiters
this is it with times inserted, not sure how to adjust it so it works-
function baseSeed = SetUpRng
rng((sum(100)times(datetime("now"))));
baseSeed = floor(rand*2^32);
rng(baseSeed, 'twister');
3 commentaires
Jon
le 18 Oct 2023
Modifié(e) : Jon
le 18 Oct 2023
What are you actually trying to do?
1) The reason you get the error message, "'*' is not supported for operands of type 'datetime'" Is that it makes no sense. What would it mean to multiply the current date and time, for example, 18-Oct-2023 08:33:51 by 100?,
2) What are you trying to calculate with sum(100), The sum(100) just equals 100
3) Since you call rng twice, even if you fixed the error with the first call to rng, whatever that did would be overwritten by the second call.
Réponses (2)
Star Strider
le 18 Oct 2023
I don’t understand summing a constant —
sum(100)
That aside, if you want to create a random datetime for a specific participant, something like this could work —
original = datetime("now")
id = datetime("now")+hours(rand*24)
or something similar.
.
2 commentaires
Steven Lord
le 18 Oct 2023
Multiplication is not defined for datetime objects. What would 2 times today represent if it were defined? I don't think there's any such definition that would be generally accepted or useful. [Addition, on the other hand, is well defined as is subtraction.]
But looking at what you're trying to do, you don't need to operate on datetime arrays anyway. Do you want to generate random numbers but don't care if the set of random numbers if repeatable, just that they're different for each participant? If so see the documentation page "Generate Random Numbers That Are Different".
rng shuffle
x = rand(1, 5)
rng shuffle
y = rand(1, 5)
x == y % all false, most likely
If you want to generate random numbers and need to be able to repeat that set of random numbers, see documentation page "Generate Random Numbers That Are Repeatable".
rng(1, "twister")
x = rand(1, 5)
rng(1, "twister")
y = rand(1, 5)
x == y % all true
0 commentaires
Voir également
Catégories
En savoir plus sur Random Number Generation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!