Borrar filtros
Borrar filtros

What is the time complexity of the function unifrnd(va​rMin,varMa​x,varSize)

2 visualizaciones (últimos 30 días)
Ali
Ali el 19 de Jun. de 2016
Comentada: John D'Errico el 19 de Jun. de 2016
Suppose varMin=-11, and varMax=11, and varSize=5. It means we generate a vector that has 5 elements, and these elements are bounded in [-11,11]. What is the time complexity? Constant, linear or what else?

Respuestas (1)

John D'Errico
John D'Errico el 19 de Jun. de 2016
Editada: John D'Errico el 19 de Jun. de 2016
No. This does NOT generate a vector. It generates a 5x5 array as you have specified the call. (If you don't believe me, try it! Or read the help.)
If you want a vector, then you need to use varSize as [1,5] or [5,1].
The bounds of the distribution have absolutely nothing to do with the time required.
As far as the time complexity, it will be linear in the number of elements to be generated. However, for a problem as small as this, by far the most amount of time will simply be function call overhead, error checks, etc.
  3 comentarios
John D'Errico
John D'Errico el 19 de Jun. de 2016
Editada: John D'Errico el 19 de Jun. de 2016
I'll admit that this was a design consideration that in hindsight, I think those who created some of these tools MIGHT reconsider, IF they were given the chance. I would agree that if just a scalar input is provided, too many people will assume it creates a vector with that number of elements, as opposed to a matrix.
But that aspect of the random generation functions goes back farther than I have been using MATLAB, so probably back to the very first release. (I first used MATLAB sometime in the mid to late 1980's, although I did have a copy of version 1 on disk somewhere around 1984 I'd guess.)
John D'Errico
John D'Errico el 19 de Jun. de 2016
A good way to test any question like this is always just to test it yourself.
timeit(@() unifrnd(-11,11,[1,1e6]))
ans =
0.015931
timeit(@() unifrnd(-11,11,[1,1e7]))
ans =
0.15783
So clearly linear in the number of elements. Of course, if I make the problem too large, things will turn bad once your machine is forced to start disk swapping to work with the array. So it will only be linear in some range. Small problems will be dominated by function overhead.
timeit(@() unifrnd(-11,11,[1,5]))
ans =
1.8231e-05
timeit(@() unifrnd(-11,11,[1,50]))
ans =
2.1712e-05
timeit(@() unifrnd(-11,11,[1,5]))
ans =
1.7175e-05
timeit(@() unifrnd(-11,11,[1,50]))
ans =
1.6973e-05
So, as you can see, the estimation of the time required has a larger uncertainty than the difference between those calls for a small problem.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by