error multiplying datetime (now) by 100 with *

3 vues (au cours des 30 derniers jours)
Laura
Laura le 18 Oct 2023
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
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.
Laura
Laura le 18 Oct 2023
Hi all,
thanks for responding.
the code just before this is
% Set up random number generator.
baseSeed = SetUpRng;
I meant to include that, sorry.
The point of this bit of code in the original Q is to-
  • set up a random number generator that will make it possible to tag the participant and rerun the exact set up of any participant's session at a later date
  • this is purely an exercise for that purpose, the x 100 , I believe , is just to make the number easier to use and work with than the output would be without it. It seems the sum is the sum of the numbers in the output given by clock before and now datetime, and that can be expressed entirely numerically, i believe.
  • this function has been used by several others and worked effectively but contained the 'clock' instead of 'datetime' so I am just following MATLAB's given advice to change to that as 'clock' is not supported on this later 2023 version, the other earlier experiments used it about a year or more ago before they replaced 'clock' with 'datetime'
  • I think the datetime we should be using here is datetime(relativeDay) as documentation says this can be specifies 'now'. The MATLAB advice stated datetime('now') to use instead of 'clock'
  • just to be clear, this doesnt really have a mathematical objective, it is right at the start of an experimental set up, to provide a (pseudo)random number that can be traced back if we need to examine the specific conditions that participant was assigned, as these vary between the participants. So if there were problems in the data entry and analysis stage, or even at pilot level we wanted to examine and check the procedure of the experiment after it ran, the time is never going to be exactly the same so it achieves a degree of randomness.
thank you for your patience, am very new to MATLAB so trying to catchup quickly and if I am unclear, I do not mean to be

Connectez-vous pour commenter.

Réponses (2)

Star Strider
Star Strider le 18 Oct 2023
I don’t understand summing a constant —
sum(100)
ans = 100
That aside, if you want to create a random datetime for a specific participant, something like this could work —
original = datetime("now")
original = datetime
18-Oct-2023 12:46:03
id = datetime("now")+hours(rand*24)
id = datetime
18-Oct-2023 12:57:29
or something similar.
.
  2 commentaires
Laura
Laura le 18 Oct 2023
thank you , I will suggest this with my supervisor
Star Strider
Star Strider le 18 Oct 2023
My pleasure!

Connectez-vous pour commenter.


Steven Lord
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)
x = 1×5
0.1368 0.8152 0.0482 0.9330 0.4625
rng shuffle
y = rand(1, 5)
y = 1×5
0.9324 0.8872 0.3880 0.6278 0.7505
x == y % all false, most likely
ans = 1×5 logical array
0 0 0 0 0
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)
x = 1×5
0.4170 0.7203 0.0001 0.3023 0.1468
rng(1, "twister")
y = rand(1, 5)
y = 1×5
0.4170 0.7203 0.0001 0.3023 0.1468
x == y % all true
ans = 1×5 logical array
1 1 1 1 1

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!

Translated by