Adding Indexes to elements of a list in kdb - kdb

How can I add to each element of a list its index, i.e add 0 to the element at index 0, add 1 to the element at index 1 and so on.
My list is list1:
q)list1:3+20?30

Use 'til' and 'count' to generate indexes and add those to actual elements.
q) list1 + til count list1
til count list1 will generate indexes of the list.
q) list1: 1 2 3
q) count list1 / 3
q) til count list1 / same as til count 3
q) 0 1 2

Related

Netlogo, iterating n items, in a list of length x (where n <= x)

I am wondering whether if its possible to iterate over n item using for loop (foreach), where the list is of length x.
For example, for a list of length 6 (x), I want to iterate the first 4 items (n) and add them to another list B.
lets say list-A is [ 7 8 12 11 5 6]
output required: list-B should be [7 8 12 11]
The code below adds all the items from list A to list B, since iteration goes through the entire list. I want to stop it at the 4th iteration. so only the first 4 items will be added.
set list-A [ 7 8 12 11 5 6]
set list-B []
let n 4
foreach list-A [
i ->
set list-B lput i list-B
]
NetLogo has sublist to extract one list from the middle of another. In your case it would look like:
to testme
let list-A [ 7 8 12 11 5 6]
let list-B sublist list-A 0 4
print list-B
end

(q/kdb+) Merge items in a list

I have a list of items and need to merge them into a single column
using the list
list:(1 2;3 4 5 7;0 1 3)
index value
0 1 2
1 3 4 5 7
2 0 1 3
my goal is
select from list2
value
1
2
3
4
5
7
0
1
3
'raze' function flattens out 1 level of the list.
q) raze (1 2;3 4 5 7;0 1 3)
q) 1 2 3 4 5 7 0 1 3
If you have list with multi level indexing then use 'over' adverb with raze:
q) (raze/)(1 2 3;(11 12;33 44);5 6)
To convert that to table column:
q) t:([]c:raze list)
ungroup would also work provided your table doesn't have multiple columns with different nesting (or strings)
q)ungroup ([]list)
list
----
1
2
3
4
5
7
0
1
3
If you just wanted your list to appear like that I would do the following.
1 cut raze list
I see that you have used a select statement, however if you want your column defined as this in your table do the following
a:raze list
tab:([] b:a)
Your output from this should look like this
q)tab
b
-
1
2
3
4
5
7
0
1
3
Overall, a more concise way to achieve what you want to do would be
select from ([]raze list)
To avoid any errors you should not call the column header 'value' as this is a protected keyword in kdb+ and when you try to reassign it as a column header kdb will through an assign error
`assign
Hope this helps

Netlogo: sequence of numbers with repeated elements

I am trying to ask Netlogo to generate a sequence of numbers with repeated elements, e.g.
[1 1 1 2 2 2 3 3 3]
I tried to use the n-values N [i -> i] syntax but it just gives a sequential list of numbers, 0 to N.
So far, I have tried using n-values primitive with sentence, e.g.
let mylist ( list sentence
n-values 3 [1] sentence
n-values 3 [2]
n-values 3 [3]
)
The problem is that this still returns a list of lists (i.e. [[1 1 1 2 2 2 3 3 3]]) and this causes problems for me later when trying to add this list into a matrix.
Thanks!
reduce sentence (map [x -> n-values 3 [x]] (range 1 4))

How to remove several items from a unsorted list in Netlogo

so i'm a bit struggling with Lists in Netlogo, so basically i've two lists and i want to remove the items that are in List 1 from List 2, for example:
List 1 : [8 6 9 7 1 3]
List 2: [5 9 8]
Resulting List : [6 7 1 3]
I've tried the following code but it returns an empty list:
if List 2 != []
[
foreach List 2
[
let p position ? List 1
if p = true
[
set List 1 remove-item p List 1
]
]
]
Any ideas ?
A combination of member? and filter will get you there:
let list1 [8 6 9 7 1 3]
let list2 [5 9 8]
let result filter [ x -> not member? x list2 ] list1
print result
Will print the desired:
[6 7 1 3]
Tip: whenever you find yourself trying to use an index for anything in NetLogo, you are probably not doing things in the optimal way. NetLogo has tons of functions (like filter, in this case) that operate on lists as a whole. There is rarely a need to explicitly loop through them.

Subtract array with different size in Matlab

If i have an array:
A[ 1
2
3
4]
and another array:
B= [1 2 3 4]
i wan to subtract each row in array A in each column of array B,
just like this:
0 1 2 3
-1 0 1 2
-2 -1 0 1
-3 -2 -1 0
each row in in array A was subtracted to each column in array B. each column here, represent each row in A,so how i will do that?
If you meant, SUBTRACT each row in array A FROM each column of array B -
bsxfun(#minus,B(:).',A(:))
If you meant, FROM each row in array A, SUBTRACT each column of array B -
bsxfun(#minus,A(:),B(:).')