merge lists alternating k items from each - kdb

given list of lists, how to merge them into a single list taking up to k items at a time from each of the lists until all items are merged?
for example, the following input and output is expected:
q)alternate[1] ("abc";"de";"fghi")
"adfbegchi"
for k~1 items at a time, the solution is:
q)mesh:{raze[y]rank x} / https://code.kx.com/phrases/sort/#mesh
q)alternate:{mesh[raze where each 1&{0|x-1}\[count each x];x]}
q)alternate ("abc";"de";"fghi")
"adfbegchi"
the above works because:
q)mesh[0 1 2 0 1 2 0 2 2;] ("abc";"de";"fghi")
"adfbegchi"
How to elegantly generalize alternate for any k<=max count each x ? python solution is here

the below should achieve this
q)f:{raze[y]iasc raze(x-1)|til'[count'[y]]};
q)f[1;("abc";"de";"fghi")]
"adfbegchi"
q)f[2;("abc";"de";"fghi")]
"abdefgchi"

Not sure if this is any faster or cleaner but a different approach:
q)alt:{(raze/)value each(s;::;)each til max count each s:x cut'y};
q)alt[1;("abc";"de";"fghi")]
"adfbegchi"
q)alt[2;("abc";"de";"fghi")]
"abdefgchi"
q)alt[2;(1 2 3 4;5 6 7;8 9 10 11;12 13 14 15)]
1 2 5 6 8 9 12 13 3 4 7 10 11 14 15
q)alt[3;(1 2 3 4;5 6 7;8 9 10 11;12 13 14 15)]
1 2 3 5 6 7 8 9 10 12 13 14 4 11 15
I was hoping to use case but unfortunately case doesn't like lists of different count (that aren't atomic values)

not sure if this is the fastest solution, substituting x for the hardcoded 1 in the alternate function above, and thus making it dyadic:
q)alternate:{mesh[raze where each x&{0|y-x}[x]\[count each y];y]}
q)alternate[2] ("abc";"de";"fghi")
"abdefgchi"
q)alternate[2] (1 2 3 4;5 6 7;8 9 10 11;12 13 14 15)
1 2 5 6 8 9 12 13 3 4 7 10 11 14 15
q)alternate[3] (1 2 3 4;5 6 7;8 9 10 11;12 13 14 15)
1 2 3 5 6 7 8 9 10 12 13 14 4 11 15
edit:
naïve benchmark comparison with #terrylynch 's alt and #Matthew Madill's f :
q)\t:10000 f[2] ("abc";"de";"fghi")
49
q)\t:10000 alternate[2] ("abc";"de";"fghi")
56
q)\t:10000 alt[2] ("abc";"de";"fghi")
87
q)\t:10000 f[3] (1 2 3 4;5 6 7;8 9 10 11;12 13 14 15)
55
q)\t:10000 alternate[3] (1 2 3 4;5 6 7;8 9 10 11;12 13 14 15)
61
q)\t:10000 alt[3] (1 2 3 4;5 6 7;8 9 10 11;12 13 14 15)
103

Related

KDB/Q: multiple PEACH?

I have a function that takes 2 parameters: date and sym. I would like to do this for multiple dates and multiple sym. I have a list for each parameter. I can currently loop through 1 list using
raze function[2020.07.07;] peach symlist
How can I do something similar but looping through the list of dates too?
You may try following:
Create list of pairs of input parameters.
Write anonymous function which calls your function and use peachon list op paired parameters
For example
symlist: `A`B`C; // symlist defined for testing
function: {(x;y)}; // function defined for testing
raze {function . x} peach (2020.07.07 2020.07.08 2020.07.09) cross symlist
I think this could work:
raze function'[2020.07.07 2020.07.08 2020.07.09;] peach symlist
If not some more things to consider. Could you change your function to accept a sym list instead of individual syms by including an each/peach inside it? Then you could each the dates.
Also, you could create a new list of each date matched with the symlist and create a new function which takes this list and does whatever the initial function did by separating the elements of the list.
q)dates
2020.08.31 2020.09.01 2020.09.02
q)sym
`llme`obpb`dhca`mhod`mgpg`jokg`kgnd`nhke`oofi`fnca`jffe`hjca`mdmc
q)func
{[date;syms]string[date],/:string peach syms}
q)func2
{[list]func[list 0;list 1]}
q)\t res1:func[;sym]each dates
220
q)\t res2:func[;sym]peach dates
102
q)
q)func2
{[list]func[list 0;list 1]}
q)dateSymList:dates,\:enlist sym
q)\t res3:func2 peach dateSymList
80
q)res3~res2
1b
q)res3~res1
1b
Let us know if any of those solutions work, thanks.
Some possible ways to do this
Can project dyadic f as monadic & parallelise over list of argument pairs
q)a:"ABC";b:til 3;f:{(x;y)}
q)\s 4
q)(f .)peach l:raze a,\:/:b
"A" 0
"B" 0
"C" 0
"A" 1
"B" 1
"C" 1
"A" 2
"B" 2
"C" 2
Or could define function to take a dictionary argument & parallelise over a table
q)f:{x`c1`c2}
q)f peach flip`c1`c2!flip l
"A" 0
"B" 0
"C" 0
"A" 1
"B" 1
"C" 1
"A" 2
"B" 2
"C" 2
Jason
I'll generalize everything, if you have a given function foo which will operate on an atom dt with a vector s
q)foo:{[dt;s] dt +\: s}
q)dt:10?10
q)s:100?10
q)dt
8 1 9 5 4 6 6 1 8 5
q)s
4 9 2 7 0 1 9 2 1 8 8 1 7 2 4 5 4 2 7 8 5 6 4 1 3 3 7 8 2 1 4 2 8 0 5 8 5 2 8..
q)foo[;s] each dt
12 17 10 15 8 9 17 10 9 16 16 9 15 10 12 13 12 10 15 16 13 14 12 9 11 11 ..
5 10 3 8 1 2 10 3 2 9 9 2 8 3 5 6 5 3 8 9 6 7 5 2 4 4 ..
13 18 11 16 9 10 18 11 10 17 17 10 16 11 13 14 13 11 16 17 14 15 13 10 12 12 ..
9 14 7 12 5 6 14 7 6 13 13 6 12 7 9 10 9 7 12 13 10 11 9 6 8 8 ..
The solution is to project the symList over the function in question, then use each (or peach) for the date variable.
If your function requires an atomic date and sym, then you can just create a new function to implement this
q)bar:{[x;y] foo[x;] each y};
datelist:`date$10?10
symlist:10?`IBM`MSFT`GOOG
function:{0N!(x;y)}
{.[function;x]} each datelist cross symlist

How to return a cross product of a binary function in KDB

I would like to explore a better way to apply binary function which iterate via each element of the two argument. Let make the question simpler by using below function as an example:
func:{x+y}
a:til 10
q)a
0 1 2 3 4 5 6 7 8 9
b:a
q)b:a
q)b
0 1 2 3 4 5 6 7 8 9
What I what to get is the cross production such that each element of the argument will cross each other and apply the function. My expected result is
0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10 11 3 4 5 6 7 8 9 10 11 12 4 5 6 7 8 9 10 11 12 13 5 6 7 8 9 10 11 12 13 14 6 7 8 9 10 11 12 13 14 15 7 8 9 10 11 12 13 14 15 16 8 9 10 11 12 13 14 15 16 17 9 10 11 12 13 14 15 16 17 18
My current solution is crossing the the list of argument first:
(func/) each (a cross b)
I wonder is there a better way to doing that? simply using func'[a;b] will just get a pairwise result which not what I want.
The following should be what you are looking for:
a +/:\: b
The same can apply for other defined functions too, for example:
a {x mod y}/:\: b
You do not need cross for this just each-right or each-left. Because '+' is a vector function you can just iterate over one list and use other list as full vector.
q) a+/:b

Matlab Special Matrix

Is there a MATLAB function to generate this matrix?:
[1 2 3 4 5 6 7 ... n;
2 3 4 5 6 7 8 ... n+1;
3 4 5 6 7 8 9 ... n+2;
...;
n n+1 n+2 ... 2*n-1];
Is there a name for it?
Thanks.
Yes indeed there's a name for that matrix. It's known as the Hankel matrix.
Use the hankel function in MATLAB:
out = hankel(1:n,n:2*n-1);
Example with n=10:
out =
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19
Alternatively, you may be inclined to want a bsxfun based approach. That is certainly possible:
out = bsxfun(#plus, (1:n), (0:n-1).');
The reason why I wanted to show you this approach is because in your answer, you used repmat to generate the two matrices to add together to create the right result. You can replace the two repmat calls with bsxfun as it does the replication under the hood.
The above solution is for older MATLAB versions that did not have implicit broadcasting. For recent versions of MATLAB, you can simply do the above by:
out = (1:n) + (0:n-1).';
My standard approach is
repmat(1:n,n,1)+repmat((1:n)',1,n)-1

Matlab : Sorting rows in decending order

I have an array A, I want to arrange each row in descending order to get a new array B. How could I do this ?
E.g.
Array A (original array):
11 9 13 10
12 4 1 6
13 5 12 11
Array B (rearranged array):
13 11 10 9
12 6 4 1
13 12 11 5
>> A=[11 9 13 10;12 4 1 6;13 5 12 11]
A =
11 9 13 10
12 4 1 6
13 5 12 11
>> sort(A,2,'descend')
ans =
13 11 10 9
12 6 4 1
13 12 11 5
For details type: help sort at Matlab command window

Change orientation of buffer function

I need a function that splits a vector in smaller frames with an overlap, like buffer, but instead of column-wise, it should be done row-wise.
This is how buffer works:
x = 1:20
x = buffer(x, 10, 5);
x = 0 1 6 11
0 2 7 12
0 3 8 13
0 4 9 14
0 5 10 15
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
What I want would be this though:
x = 0 0 1 2
1 2 3 4
3 4 5 6
5 6 7 8
7 8 9 10
9 10 11 12
11 12 13 14
13 14 15 16
15 16 17 18
17 18 19 20
Is there any function or way to achieve that? Maybe combination of buffer + some rearranging?
First figure out the answer in columns, then transpose the resulting matrix:
buffer(x, 4, 2).'