Obtain separate columns in a matrix

16 views Asked by At
#0 #1 #2 #3 #4 #5 #6 #7 #8 #9  #10 #11
-- -- -- -- -- -- -- -- -- --- --- ---
1  11 21 31 41 51 61 71 81 91  101 111
2  12 22 32 42 52 62 72 82 92  102 112
3  13 23 33 43 53 63 73 83 93  103 113
4  14 24 34 44 54 64 74 84 94  104 114
5  15 25 35 45 55 65 75 85 95  105 115
6  16 26 36 46 56 66 76 86 96  106 116
7  17 27 37 47 57 67 77 87 97  107 117
8  18 28 38 48 58 68 78 88 98  108 118
9  19 29 39 49 59 69 79 89 99  109 119
10 20 30 40 50 60 70 80 90 100 110 120

I can access consecutive columns at the position 1-4 using m[:, 1:5] in DolphinDB, but it cannot work when retrieving separate columns. For example, I’d like to obtain the columns at the position 1, 9, and 10, but m[:,[1,9,10]] returns an error message:

 colIndex must be a scalar value or a range

How can I access separate columns in a matrix in DolphinDB?

1

There are 1 answers

0
YaN On

You can access the matrix with m[1,9,10] to retrieve the columns:

m=1..120$10:12;
index= 1 9 10
m[index]

output

#0 #1  #2 
-- --- ---
11 91  101
12 92  102
13 93  103
14 94  104
15 95  105
16 96  106
17 97  107
18 98  108
19 99  109
20 100 110

To obtain separate rows (e.g. rows 1, 3 and 5):

m[1 3 5,0..(cols(m)-1)]

output

#0 #1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11
-- -- -- -- -- -- -- -- -- -- --- ---
2  12 22 32 42 52 62 72 82 92 102 112
4  14 24 34 44 54 64 74 84 94 104 114
6  16 26 36 46 56 66 76 86 96 106 116

To obtain specific cells:

m[1 3 5,1 9 10]

#0 #1 #2 
-- -- ---
12 92 102
14 94 104
16 96 106

DolphinDB version 2.00.8 also offers a new function loc which has the same function as pandas loc. For example:

m.loc(colFilter=(1..12 in[1,9,10]), view=false)