Sorry for my dumb question, but
how can I get the size of a set (the max index number)?
Is there a built in function, or i have to count it in a loop?
The command you're looking for is nops.
That's short for "number of operands", and it also works for lists.
Related
I need to calculate a pseudo random number in a given range (e.g. 0-150) based on another, strictly increasing number. Is there a mathematical way to solve this?
I am given one number x, which increases by 1 every day. Based on this number, I need to - somehow - calculate a number in a given range, which seems to be random.
I have a feeling that there is an easy mathematical solution for this, but sadly I am not able to find it. So any help would be appreciated. Thanks!
One sound way to do that is to hash the number x (either its binary representation or in text form) and then to use the hash to produce the 'random' number in the desired range (say by taking the first 32 bits of the hash and extracting by any known method the desired value). A cryptographic hash can be used like Sha256, but this is not necessary, MurmurHash is possibly a good one for your application.
Normally when you generate a random number, a seed value is used so that the same sequence of psuedorandom numbers isn't repeated. When a seed isn't explicitly given, many systems will use the time as a seed value.
Perhaps you could use x as a seed.
Here's an article explaining seeding: https://www.statisticshowto.com/random-seed-definition/
I'm having trouble interpreting the clingo v 5.2.2 output below:
Why did clingo print answer1 twice? I've checked that the two answer1s are exactly the same.
I'm using optimization in my code. Could that be the problem?
I've set up a test repo. The above output can be generated by running the following line in the test repo.
clingo --opt-mode=OptN examples/swap-shift.lp
Here's what I understand now: with the --opt-mode=OptN option, clingo will try multiple models until it finds all the optimal models, and it will print all the models it has witnessed followed by the optimal models. I think that could explain why answer1 was printed twice in my original question.
In the output, I think Models is the number of total witnesses (including the first optimal model that got printed twice). Optimal is the number of optimal models found.
I made my observation by observing the output of this example
clingo --opt-mode=OptN asp/_all.lp asp/examples/scatter.lp
See how Optimization decreases and the two optimum models are printed in the end.
While looking for an optimal answer set, clingo prints each answer set that is better than previously found answer sets.
If no better answer set can be found, this means that the optimality of the last answer set has been proved.
If you have use the optN mode, clingo then enumerates all answer sets with that optimal cost. This will again find the first optimal answer set that was found.
I personally do not know how to print only the optimal answer sets and each only one from the command line. It would be possible to implement such a behavior using the Python API.
As per official guide, you can use the flags --opt-mode=optN --quiet=1 to find and print all optimal solutions.
To compute all optimal answer sets, we can change clasp’s
optimization mode using option ‘--opt-mode=optN’. In this mode, clasp
first prints the tenta- tive answer sets where optimality is not yet
proven and afterwards prints the optimal answer sets. Note that the
first optimal answer set is printed twice in this mode. To omit
tentative answer sets in the output and only print optimal answer
sets, we can add option ‘--quiet=1’
From the Potassco guide version 2.2.0.
The Matlab documentation seems unclear about how to ignore missing data when using kruskalwallis, the Kruskal-Wallis (or any other related) test. The same goes for unequal group size.
Very late answer, but I ran into the same problem myself today, might as well help some future searcher.
The solution is pretty straightforward. kruskalwallis is primarily used on matrices and by default compares equal-sized columns, but it does allow you to instead assign groups manually, with the optional variable "group". I was attempting to check whether a single value was unlikely to belong to a distribution from a different set, so this was straightforward. I just added the value I wanted to test on to the end of the set I was testing against, then made "group" a vector of ones the same size as the set, with a "2" added to the end for the new value. Looks like it worked quite nicely.
For numeric data, the the standard missing data value in Matlab is NaN. See ismissing. See also this article from The MathWorks. For tables, you might find standardizeMissing helpful as well as replaceWithMissing for dataset objects. I can't say anything about group size.
I read that it's possible to make quicksort run at O(nlogn)
the algorithm says on each step choose the median as a pivot
but, suppose we have this array:
10 8 39 2 9 20
which value will be the median?
In math if I remember correct the median is (39+2)/2 = 41/2 = 20.5
I don't have a 20.5 in my array though
thanks in advance
You can choose either of them; if you consider the input as a limit, it does not matter as it scales up.
We're talking about the exact wording of the description of an algorithm here, and I don't have the text you're referring to. But I think in context by "median" they probably meant, not the mathematical median of the values in the list, but rather the middle point in the list, i.e. the median INDEX, which in this cade would be 3 or 4. As coffNjava says, you can take either one.
The median is actually found by sorting the array first, so in your example, the median is found by arranging the numbers as 2 8 9 10 20 39 and the median would be the mean of the two middle elements, (9+10)/2 = 9.5, which doesn't help you at all. Using the median is sort of an ideal situation, but would work if the array were at least already partially sorted, I think.
With an even numbered array, you can't find an exact pivot point, so I believe you can use either of the middle numbers. It'll throw off the efficiency a bit, but not substantially unless you always ended up sorting even arrays.
Finding the median of an unsorted set of numbers can be done in O(N) time, but it's not really necessary to find the true median for the purposes of quicksort's pivot. You just need to find a pivot that's reasonable.
As the Wikipedia entry for quicksort says:
In very early versions of quicksort, the leftmost element of the partition would often be chosen as the pivot element. Unfortunately, this causes worst-case behavior on already sorted arrays, which is a rather common use-case. The problem was easily solved by choosing either a random index for the pivot, choosing the middle index of the partition or (especially for longer partitions) choosing the median of the first, middle and last element of the partition for the pivot (as recommended by R. Sedgewick).
Finding the median of three values is much easier than finding it for the whole collection of values, and for collections that have an even number of elements, it doesn't really matter which of the two 'middle' elements you choose as the potential pivot.
I have written matlab codes for two different block matching algorithms, extensive search and three step search, but i am not sure how i can check whether i am getting the correct results. Is there any standard way to check these or any standard code which i can run and compare my result with.I read somewhere that JM software can be used but i didnt find any way to use it.
You can always use the results produced by your algorithms to create the next frame of video and then analyze its quality by either visually inspecting it (which is rather subjective, and we like to deal in numbers) or calculating the mean square error between the produced image and the one you're trying to estimate. Mean square error of the exhaustive (extensive) search should be lower than the one three-step gives you.
Well, did you try to plot it? I mean,after the block-matching you have a new image, right?.
A way to know if you result if true or not is to check the sum of the difference of 2 frames.
A - pre_frame
B - post_frame
C - Compensated frame
If abs(abs(A-B)) is lower than abs(abs(A-C))) that mean it could be true.
Next time, try to specify your algoritm. Also, put your code here to help you more.