error multiplying datetime (now) by 100 with *
Mostra commenti meno recenti
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 Commenti
Dyuman Joshi
il 18 Ott 2023
Modificato: Dyuman Joshi
il 18 Ott 2023
The function times, .* does multiplication as well, but that is element-wise multiplication. The correct syntax is
rng(times(sum(100),datetime("now")))
%or
%rng(sum(100).*datetime("now"))
Which begs the following questions - Why are you multiplying a datetime value? What are you trying to do? What are you expecting the output to be?
"the set up for a random number generator, to keep an accurate and repeatable timestamp of each participant in a psychophysics experiment"
Could you please elaborate on this? Preferably by providing information about the mathematics of the objective.
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.
Laura
il 18 Ott 2023
Risposte (2)
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.
.
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
Categorie
Scopri di più su Data Type Conversion in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!