Perl, Compare and calculate the occurance of intersection between 2 arrays. - perl

assume the following situations:
I have two arrays: array1 and array2; both holding very large number of elements inside. There can be intersection element between these two arrays and inside each array, the elements can be either unique or duplicated. Now that I want to calculate the number of occurance for all those intersection elements. I know that I can use nested loop to traverse and compare each element inside both array, but see, since each of them is holding a large number of elements, the run speed is going to be terrible. I need to run this program frequently, so is there any faster way to achieve same purpose? Thanks a lot!

You no need using nested loop. Just one for loop is enough to solve your problem. Below is the example but i not sure how you want to pass the array into scripts so i just put as your array1.
use strict;
use warnings;
my $i = 0;
my #array1 = "your array1";
my #array2 = "your array2";
for($i=0;$i<=$#array1;$i++){
my $element_in_array1 = $array1[$i];
my #occurrence = grep=/$element_in_array1/, #array2;
print " This element $element_in_array1 occur in array2 %d time\n", scalar(#occurence);
}

Related

perl; what is this? for(1..$scalar_name)

I'm reading a perl code to distill what it's doing, but can't figure out what 1..$scalar_name in these lines is doing
my $scalar_name = scalar #array_name;
push #zeroes, 0 for(1..$scalar_name);
Thank you!
Two dots .. is a range operator.
Binary ".." is the range operator, which is really two different operators depending on the context. In list context, it returns a list of values counting (up by ones) from the left value to the right value. If the left value is greater than the right value then it returns the empty list. The range operator is useful for writing foreach (1..10) loops and for doing slice operations on arrays.
Your code takes the number of elements of the array and creates a new array with the same number of zeros.

Minizinc, counting occurrences in array of pairs

I'm new to constraint programming and toying around with some basic operations. I want to count the number of occurrences of an arbitrary element x in an array of pairs.
For instance, the following array has 2 eights, and 1 of every other element.
sampleArray = [{8,13}, {21,34}, {8,55}]
I wonder how I am to extract this information, possibly using built-in functions.
I'm not sure I understand exactly what you want to do here. Do you want to count only the first element in the pair?
Note that the example you show is an array of sets, not a 2 dimensional matrix. Extracting and count the first(?) element in each pair is probably easier if you have a two dimensional matrix (constructed with array2d).
In general there are at least two global constraints that you can use for this: "count" and perhaps also "global_cardinality". See http://www.minizinc.org/2.0/doc-lib/doc-globals-counting.html

Insertion Sort Algorithm In place and loop variant

Part 1
I know that QuickSort can be used 'in place' but could someone explain to me how Insertion sort Algorithm does this using 'in place'.
From my understanding:
Insertion Sort starts at the first value and compares it to the next value, if that value is less than our value they switch positions. We continue this recursively. (Short explanation)
So would you say that this is 'in place' because we don't create a new array to do this, but just compare two elements in an array?
If my understanding was wrong could someone please explain the
algorithm for insertion sort in place.
Part 2
Also how would I use insertion sort to illustrate the idea of a loop invariant?
I know that a loop invariant is a condition that is immediately true before and after each iteration of a loop but I'm not sure how this would relate to an insertion sort.
Like Thorsten mentioned in the comments section, you have described bubble sort. Modified from Wikipedia, pseudocode for bubble sort is as follows:
procedure bubbleSort( A : list of sortable items )
n = length(A)
for i = 1 to n inclusive do // Outer Loop
for j = 1 to n-1-i inclusive do
/* if this pair is out of order */
if A[j] > A[j+1] then
swap(A[j], A[j+1])
end if
end for
end for
end procedure
The loop invariant in bubble sort (for the outer loop) would be that, after each iteration of the loop, the entire array until the current value of i would be sorted. This is because, each time one reaches the outer loop, it will be after going through all iterations of the inner loop (from i to n-1), finding the minimum element there and swapping that element with the ith one.
Bubble sort is, indeed, in place, since all the sorting happens within the original array itself, and does not require a separate external array.
Edit- now onto insertion sort:
Pseudo code is as follows (all hail Wikipedia):
for i = 1 to length(A) - 1
x = A[i]
j = i
while j > 0 and A[j-1] > x
A[j] = A[j-1]
j = j - 1
end while
A[j] = x[3]
end for
Here, at each step, what happens is that for each element, you select the appropriate location at which to insert it into the array, i.e., you insert it just after the first element that is smaller than it in the array. In a little more detail, what the inner loop does is that, it keeps shifting elements to the right till it encounter an element smaller than the element in consideration, at which point you insert the element just after the smaller element. What this will mean is that every element until the aforementioned element is sorted. the outer loop ensures that this is done for all elements within the array, which means that by the time the outer loop completes, the array is sorted.
The loop invariant for the outer loop is, like before, that after the ith iteration, all elements till the current i will be sorted. Just before the ith interation, however, all elements till i-1 will be sorted.
The insertion sort algorithm does not require an external array for sorting. More specifically, all operations are done on the array itself (except for the one variable we need to store the element that we are currently trying to insert into its appropriate location), and no external arrays are used- there is no copying from this array to another one, for example. So, the space complexity required by the algorithm (excluding, of course, the space the array itself occupies) will be O(1), as opposed to dependent on the size of the array, and the sort is in-place, much like bubble sort.

find all possible element combinations containing the last element in matlab

I have a vector [x1, x2,...xn]. Is there a way to find all possible combinations of elements that contain the last element xn? For example, if I have 4 elements I want the combinations:
x1,x4
x2,x4
x3,x4
x1,x2,x3,x4
x1,x2,x4
x1,x3,x4
x2,x3,x4
In reality though I have number of elements up to a few hundreds.
Thank you for your time!
You really just need to do a choose on all of the elements except the last one.
C = cell(length(x)-1,1);
for n = 1:length(x)-1
C{n} = nchoosek(x(1:end-1),n);
end
Each element of C contains all possible vectors with n elements. All you have to do is tack onx(end) to each one to get what you're looking for. For example, if combo=C{4}(7,:) is one solved set without the last element of x, then your desired output is combo=[combo x(end)]. To do this for all solutions, just add this line of code inside the loop above:
C{n} = [C{n} x(end)*ones(size(C{n},1),1)];
WARNING: With thousands of elements you will run out of memory very quickly. Just 100 elements gives you over 6e29 possible combinations!

vector of variable length vectors in MATLAB

I want to sum up several vectors of different size in an array. Each time one of the vectors drops out of my program, I want to append it to my array. Like this:
array = [array, vector];
In the end I want to let this array be the output of a function. But it gives me wrong results. Is this possible with MATLAB?
Thanks and kind regards,
Damian
Okay, given that we're dealing with column vectors of different size, you can't put them all in a numerical array, since a numerical array has to be rectangular. If you really wanted to put them in the numerical array, then the column length of the array will need to be the length of the longest vector, and you'll have to pad out the shorter vectors with NaNs.
Given this, a better solution would be, as chaohuang hinted at in the comments, to use a cell array, and store one vector in each cell. The problem is that you don't know beforehand how many vectors there will be. The usual approach that I'm aware of for this problem is as follows (but if someone has a better idea, I'm keen to learn!):
UpperBound = SomeLargeNumber;
Array = cell(1, UpperBound);
Counter = 0;
while SomeCondition
Counter = Counter + 1;
if Counter > UpperBound
error('You did not choose a large enough upper bound!');
end
%#Create your vector here
Array{1, Counter} = YourVectorHere;
end
Array = Array(1, 1:Counter);
In other words, choose some upper bound beforehand that you are sure you won't go above in the loop, and then cut your cell array down to size once the loop is finished. Also, I've put in an error trap in case you're choice of upper bound turns out to be too small!
Oh, by the way, I just noted in your question the words "sum up several vectors". Was this a figure of speech or did you actually want to perform a sum operation somewhere?