kdb+ Q - return sum of integers contained in a list - kdb

I have a simple list of integers, how do i return the sum of all integer values contained within the list?
a:(5;10;15)
thanks
update: i had another list b:(2;4;6), and i joined both lists into one c:(a;b), now when i do sum c, it throws an error, any ideas?
solved: i used 'raze' when joining lists - c:raze(a;b) then sum c

Related

how to concate list of strings by element

I have two lists for example:
x:("AA","BB","CC")
y:("1","2","3")
I would like to target the concatenation of both lists element wise as below:
z = ("AA1","BB2","CC3")
I have tried the following which only works if the lists have one string:
(x,y)
Use eachboth adverb which takes one element from each list at a time and perform operation.
Also change comma to semicolon in your x and y list to get list with 3 items.
q) x:("AA";"BB";"CC")
q) y:("1";"2";"3")
q) x,'y
Output:
("AA1";"BB2";"CC3")

Scala create array of empty arrays

I am trying to create an array where each element is an empty array.
I have tried this:
var result = Array.fill[Array[Int]](Array.empty[Int])
After looking here How to create and use a multi-dimensional array in Scala?, I also tried this:
var result = Array.ofDim[Array[Int]](Array.empty[Int])
However, none of these work.
How can I create an array of empty arrays?
You are misunderstanding Array.ofDim here. It creates a multidimensional array given the dimensions and the type of value to hold.
To create an array of 100 arrays, each of which is empty (0 elements) and would hold Ints, you need only to specify those dimensions as parameters to the ofDim function.
val result = Array.ofDim[Int](100, 0)
Array.fill takes two params: The first is the length, the second the value to fill the array with, more precisely the second parameter is an element computation that will be invoked multiple times to obtain the array elements (Thanks to #alexey-romanov for pointing this out). However, in your case it results always in the same value, the empty array.
Array.fill[Array[Int]](length)(Array.empty)
Consider also Array.tabulate as follows,
val result = Array.tabulate(100)(_ => Array[Int]())
where the lambda function is applied 100 times and for each it delivers an empty array.

How to select rows from character array that match char string and save them in a new array?

I have a char array A which basically contains a list of files names (each row one file)
(char, 526x26)
val =
0815_5275_UBA_A_1971.txt
0815_5275_UBA_A_1972.txt
0823_6275_UBA_A_1971.txt
0823_6275_UBA_A_1972.txt
0823_6275_UBA_A_1973.txt
...
I also have a variable
B = '0815_5275'
I'd like to select all rows (filenames) that start with B and save them in a new array C.
This should be simple, but somehow I can't make it work.
I've got this:
C = A(A(:,1:9) == B);
but I get the error message:
Error using ==
Matrix dimensions must agree.
I do not know in advance how many rows will match, so I can not pre-define an empty array.
thanks, any help is appreciated!
Try ismember(A(:, 1:numel(B)), B, 'rows') rather to get a logical vector that indexes only the rows you want
and now
A(C,:) to extract the rows
The reason you're getting a dimension mismatch error is because your A(:,1:9) has many rows but B only has one and Matlab does not automatically broadcast like Octave or Python. You could do it using either repmat or bsxfun but in this case ismember is the correct function to choose.

select items from a struct array matlab- based on condition

I have a struct array named Lst. Every struct has the following form:
Point (x,y)
Type (1-6)
I want get the separate array of points for each type. How can I get it?
Lst(Lst.Type==1);
won't work since Type is not a field of Lst but of Lst(i).
In addition, is there a way to save the indexes of each item or an alternative way to then combine them again to the original order?
L1 = Lst([Lst.Type]==1); will give you the subset L1 of Lst where Type == 1.
Likewise, you can use idx1 = find([Lst.Type]==1) to memorize your indexes.
EDIT: the above uses the [] operator to aggregate the field elements Type of Lst into an array. To your comment/question, you could use the exact same operator also to obtain an array of specific field elements X of a subset of the structured array, as in
X1 = [Lst([Lst.Type]==1).X];

How to get a list of all structs where struct.somearray contains an element from a query array

As input to a function, I am getting an array of target elements, T, and an array of structs S where each one has a .elems field, which is a list of integers (elements).
I'm sure there's a simple way to do this in Matlab. How do I get the indices i of all structs where a specific element t of T is in S(i).elems contains t?
So I think you'll need to do this with an arrayfun. I did:
S = ... (1-by-N array of structs);
T = ... (1-by-K array of numbers);
indices = find(arrayfun(#(i)any(ismember(T, S(i).elems)), 1:numel(S)));
any(ismember(T, S(i)elems)) tests is any of the things in T are in S(i).elems. The arrayfun repeats this for each struct in S. find extracts indices from the logical array that is returned by the arrayfun.