Sum of each two elements using vector functions - kdb

How to get sum of eath two elements with the vector functions? I want the same result as:
{x+y}':[1 2 3 2 1]
Why this approach gives something different than first one?
sum':[1 2 3 2 1]

sum is not the same as {x+y}.
sum has rank 1 meaning it takes one input and sums the elements of that input.
It can sum an atom:
q)sum 1
1
a uniform list
q)sum 1 2
3
or a list of lists
q)sum(1 2;3 4)
4 6
{x+y} is rank 2 meaning it requires two inputs.
q){x+y}[1;2]
3
q){x+y}[1 2;3 4]
4 6
Giving it an atom, a single list, or a list of lists leads to projections
q){x+y}1
{x+y}[1]
q){x+y}1 2
{x+y}[1 2]
q){x+y}(1 2;3 4)
{x+y}[(1 2;3 4)]
Since each-prior (':) creates binary pairs from the input and attempts to apply a rank 2 function, it works as intended on your rank 2 function {x+y}.
But since sum is not rank 2 the each-prior doesn't generate pairs in the same way, it's equivalent to doing
q){x}':[1 2 3 2 1]
1 2 3 2 1
q){sum x}':[1 2 3 2 1]
1 2 3 2 1
You could force it to be rank 2:
q){sum(x;y)}':[1 2 3 2 1]
1 3 5 5 3
but this gives a different result since sum ignores nulls while + doesn't.
q)sum(0N;1)
1
q)0N+1
0N
Finally, an alternative way to achieve this using sum (and without using each-prior) is to shift the vector using prev and then sum
q){sum(prev x;x)}[1 2 3 2 1]
0N 3 5 5 3

Related

Element-wise concatenation of 2 vectors in kdb+

Given 2 vectors A and B, I want to concatenate each element of A with each element of B. For example if A and B were as follows:
A: 0 1 2
B: 3 4 5
then the output should be (0 3;1 4; 2 5)
Joining the two vectors using the each (each-both in this case) iterator returns your desired output.
q)0N!A,'B
(0 3;1 4;2 5)
0 3
1 4
2 5
You could also instantiate through the following
(A;B) to create a 2x3 matrix which can be flipped to get what you require
q)A:0 1 2
q)B:3 4 5
q)(A;B)
0 1 2
3 4 5
q)flip (A;B)
0 3
1 4
2 5

How to implement combinations of a list

All
I need to get the combinations and permutations of a list.
A function have been implemented for permutations.
perm:{[N;l]$[N=1;l;raze .z.s[N-1;l]{x,/:y except x}\:l]}
However, I have no idea about combinations, just like this:
l: 1 2 3
comb[2;l]
1 2
1 3
2 3
l: 1 2 3 4
comb[3;l]
1 2 3
1 2 4
1 3 4
2 3 4
Thanks!
From your solution, you can do:
q)comb:{[N;l]$[N=1;l;raze .z.s[N-1;l]{x,/:y where y>max x}\:l]}
q)comb[2;1 2 3]
1 2
1 3
2 3
Another approach using over:
q)perm:{{raze x{x,/:y except x}\:y}[;y]/[x-1;y]}
q)comb:{{raze x{x,/:y where y>max x}\:y}[;y]/[x-1;y]}
One option is to use your permutation function like this:
q) comb:{[N;l] distinct asc each perm[N;l] }
q)l: 1 2 3 4
q) comb[3;l]
output:
1 2 3
1 2 4
1 3 4
2 3 4
Note: This will change the order of elements because of asc. So if your list should have (1 3 2) in answer, it will give (1 2 3).
To maintain order, use any other function/logic in place of asc to filter duplicate elements in sets (ex: (1 2 3) and (1 3 2) are duplicates).

finding rows with specific values for a column (matlab)

I Have a matrix in matlab,for example
A=[1 2 3
1 3 5
1 4 2
2 3 1
2 4 3]
and an array like this:
b=[3
4]
now I want to find rows in A, that the second column equals one of the values in b. In this example:
Result=[1 3 5
1 4 2
2 3 1
2 4 3]
I want to find this without using loop.
That's easy: use the ismember function:
Result = A(ismember(A(:,2),b),:);
You could also use bsxfun:
Result = A(any(bsxfun(#eq, A(:,2).', b(:)),1),:)

find group of elements in matrix matlab

I have an array of elements E = [1 2 3 4 5 10] and matrix A of couples of elements
A=[ 1 2;
2 3;
3 1;
4 1;
4 3;
5 1]
You can read this matrix as
1 is similar (with a certain probability) to 2
2 is similar (with a certain probability) to 3
3 is similar (with a certain probability) to 1 etc...
I would like to know how many different groups of element are in the array E given the matrix A
In this case I have
1 group = 1 2 3
2 group = 1 3 4
3 group = 1 5
4 group = 10
so there are 4 different groups of elements in E
NOTE:
if 1 is similar to 2 (first row)
and if 5 is similar to 1 (sixth row)
5 is not similar to 2!!! (if it is not written anywhere)
so 1 2 5 don't belong to the same group
On the other hand
1 is similar to 2 (first row),
2 is similar to 3 (second row)
and 3 is similar to 1 (third row)
so 1 2 3 belong to the same group

Get first two maximum values in matrix

I've got a matrix (n x m). And I'd like to know, for each row, the indexes of the coloums that contain the first two maximum values:
2 3 4 2
2 4 7 1
1 1 2 4
5 5 9 6
1 4 2 1
9 8 1 2
The answer should be:
2 3
2 3
3 4
3 4
2 3
1 2
How can I obtain it with matlab commands? I'd like not to use for loops. I tried with:
[x,y]=max(matrix')
y=y';
y gives me the colum indexes for the maximum elements. Now I'd set to zero these elements and repeat the instructions but I have no idea how to do. I treid:
matrix(:,y)=0;
but it doesn't work.
if A is your matrix, then sort and pick the top two indices,
[a ix]=sort(A,2)
ans= ix(:,end-1:end)