I have showing value of 5 categories month-wise (4 months) in matrix like as follow:
CategoryA 1 2 3 4
CategoryB 4 5 8 0
CategoryC 2 4 6 7
CategoryD 9 5 4 5
CategoryE 5 4 8 5
Now I have added 1 column (Outside group) in right showing total row-wise. What I am going to ask is that I want to add a row (outside group) to show percentage (Category D / CategoryA * 100) month wise (against all 4 column and "total" Column)
Please suggest .....
You can give a try to the following steps:
Create a calculated column let’s say CategoryDValue in the dataset and use this expression =IIF(Fields!Category.Value=” Category D”,Fields!MonthData.Value,0)
Now in the outside group column cell use this expression =(SUM(Fields! CategoryDValue.Value) / SUM(Fields!MonthData.Value)) * 100
Related
There are already moving average in kdb/q.
https://code.kx.com/q/ref/avg/#mavg
But how do I compute moving median?
Here is a naive approach. It starts with an empty list and null median and iterates over the list feeding in a new value each time.
Sublist is used fix the window, and this window is passed along with the median as the state of into the next iteration.
At the end scan \ will output the state at every iteration from which we take the median (first element) from each one
mmed:{{(med l;l:neg[x] sublist last[y],z)}[x]\[(0n;());y][;0]}
q)mmed[5;til 10]
0 0.5 1 1.5 2 3 4 5 6 7
q)i:4 9 2 7 0 1 9 2 1 8
q)mmed[3;i]
4 6.5 4 7 2 1 1 2 2 2
There's also a generic "sliding window" function here which you can pass your desired aggregator into: https://code.kx.com/q/kb/programming-idioms/#how-do-i-apply-a-function-to-a-sequence-sliding-window
q)swin:{[f;w;s] f each { 1_x,y }\[w#0;s]}
q)swin[avg; 3; til 10]
0 0.33333333 1 2 3 4 5 6 7 8
q)update newcol:swin[med;10;mycol] from tab
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
i have data A=( 3,5,3,1,4 ) in a column and
B=[
4 6 9 1 3
2 7 2 5 7
7 3 1 8 2
4 1 6 9 1
2 5 8 3 6 ]
And i want: as in A first element is 3 and for this i want to get first element of column 3 row 1 from B which is 9. The second element of A is 5 and for this i want to get the the 2nd element of column 5 and row 2 from B which is 7 ,and do the process for all other elements . how to do this in matlab? the required elements are bold and underlined. The desired output is [9,7,1,4,3]
Read about linear indexes.
sub2ind will convert from [row col] to index.
Cols=[ 3,5,3,1,4 ];
Rows=1:length(Cols);
B=[
4 6 9 1 3
2 7 2 5 7
7 3 1 8 2
4 1 6 9 1
2 5 8 3 6 ];
Indexes=sub2ind(size(B),Rows,Cols);
Vals=B(Indexes)
if I've read well you want an elements replacement. It's quite simple
A(1)=B(1,3)
A(2)=B(2,5)
So after you have declared your two vectors you can handle them replacing singular components. In general when you have a 1-D vectors you can access to it's component position only declaring the position itself inside the parentheses, as I did for A.
Whe you have to face situation like in B, If you remember linear algebra and matrix in general B(a,b) means the element of matrix B placed in the line a and column b so you have to specify row and column to access that element.
I would like to select a sub-matrix of dimension 1000rows X 10columns conditional on the value of a certain column and move this sub-matrix to a different (new) matrix.
The problem is that I would like to do that for millions of observations, with sub-matrix having dimensions approximately equal to 1000rows X 10 columns (the dimension might change as well from sub-matrix to sub-matrix, it can be for instance 950 X 10, then 1050 X 10). I would like to create as many new matrices as the number of my sub-matrices.
1 2 3
-----------
1 1 2 2
2 2 2 2
3 3 2 2
4 4 2 2
5 5 2 2
6 5.5 2 2
7 6 2 2
8 6.5 2 2
9 7 2 2
10 8.5 2 2
11 12 2 2
12 15 2 2
For example:
In the above matrix, I would like to select all the elements conditional on the fact that the elements of column 1 are between 5 and 10 (i.e. rows 5 to 10), and then create a new matrix. But I have to do that for millions of sub-matrices, so I would have to create millions of new matrices.
I have the matrix as follows
a =
1 3
2 5
3 2
4 8
5 9
I want to sort the second column in the a matrix. I want the corresponding rows of column one to be printed as follows :
a =
3 2
1 3
2 5
4 8
5 9
I tried sort(a), but it is sorting only the second column of matrix a.
Try this:
sortrows(a,2)
This should sort according to the second column.
or use:
[val idx]=sort(a(:,2));
ans = [a(idx,1) val]