Count the first x integers of a group of values - kdb

I want to get the count starting from 1, of the number of occurrences of Y until its subsequent value is N. A simple example table can be found below, I've added an additional column called expected output to highlight what I'm trying to achieve.
tab:([]x:`N`N`Y`N`N`Y`Y`Y`N`N`Y`Y`Y;expected_output:0 0 1 0 0 1 2 3 0 0 1 2 3)
I have been playing around with the idea of trying to use cut (granted that I can find the correct indexes) I could split the table up, get the count of each list, then piece it all back together somehow e.g.
0 2 3 5 8 10 cut tab

Approach without scan, not as neat as terry's but should be faster on larger tab.
q)update o:{a+r-maxs differ[x]*r:sums a:`Y=x}x from tab
x expected_output o
-------------------
N 0 0
N 0 0
Y 1 1
N 0 0
N 0 0
Y 1 1
Y 2 2
Y 3 3
N 0 0
N 0 0
Y 1 1
Y 2 2
Y 3 3

One approach using scan
q)update c:0{y*x+y}\x=`Y from tab
x expected_output c
-------------------
N 0 0
N 0 0
Y 1 1
N 0 0
N 0 0
Y 1 1
Y 2 2
Y 3 3
N 0 0
N 0 0
Y 1 1
Y 2 2
Y 3 3
Essentially a modified version of sums which resets the counter back to zero (using zero/false multiplication) whenever the next boolean is zero

Related

Create a matrix in MATLAB with limited number of elements

I would like to create an 12*3 matrix in MATLAB that has only 2 non-zero elements in each row. How should I generate a code to get all the possible conditions. The non-zero elements can take on any integers from 1 to 2.
If you want all the possible combination without repetition for 0 and with repetition for 1 and 2:
% Number of integer with repetition.
n = 2
% Generate all the possible combination of 1 and 2.
[x1,x2] = meshgrid(1:n,1:n);
M = [zeros(n^2,1),x1(:),x2(:)];
% We shift the 0 column n time.
M = cell2mat(arrayfun(#(x) circshift(M,x,2),0:n,'UniformOutput',0).');
Result:
M =
0 1 1
0 1 2
0 2 1
0 2 2
1 0 1
2 0 1
1 0 2
2 0 2
1 1 0
1 2 0
2 1 0
2 2 0

Generate all possible column vectors in matlab

I am essentially trying to figure out how to generate code for basis vectors of different configurations of M objects into N different states (for example, if I had 2 snacks between 2 kids, I could have (2,0) (0,2) or (1,1), terrible example, but thats the idea)
I am struggling to figure out how to do this without going into many different loops (I want this to be automatic). The idea would be to create a Matrix where each row is a vector of length M. I would start with vec(1) = N then an if loop where if sum(vec) == N, Matrix(1,:)=vec; Then I could take vec(1)=N-i and do the same.
My only issue is I do not see how to use the if and forget it so that if I had maybe 2 objects in 5 locations, how would I do this to get (1 0 0 0 1).
I am not seeing how to do this.
You could use a recursive function:
function out = combos(M,N)
if N == 1
out = M;
else
out = [];
for i = 0:M
subout = combos(M-i,N-1);
subout(:,end+1) = i;
out = [out;subout];
end
end
I think this does what you want.
The key idea is to generate not the number of elements in each group, but the split points between groups. This can be done via combinations with repetition. Matlab's nchoosek generates combinations without repetition, but these are easily converted into what we need.
M = 5; % number of objects
N = 3; % number of groups
t = nchoosek(1:M+N-1, N-1); % combinations without repetition...
t = bsxfun(#minus, t, 1:N-1); % ...convert into combinations with repetition
t = diff([zeros(size(t,1), 1) t repmat(M, size(t,1), 1) ], [], 2); % the size of each
% group is the distance between split points
In this example, the result is
t =
0 0 5
0 1 4
0 2 3
0 3 2
0 4 1
0 5 0
1 0 4
1 1 3
1 2 2
1 3 1
1 4 0
2 0 3
2 1 2
2 2 1
2 3 0
3 0 2
3 1 1
3 2 0
4 0 1
4 1 0
5 0 0
This is a similar approach to Luis' without bsxfun. Because we don't like fun.
n = 5;
k = 3;
c = nchoosek(n+k-1, k-1);
result = diff([zeros(c, 1) nchoosek(1:(n+k-1), k-1) ones(c, 1)*(n+k)], [], 2) - 1;
This creates the partitions of the integer n with length k. Given an array of length n + (k-1), we find all combinations of (k-1) places to place partitions between the (unary) integers. For 5 items and 3 locations, we have 7 choices of where to put the partitions:
[ 0 0 0 0 0 0 0 ]
If our chosen combination is [2 4], we replace positions 2 and 4 with partitions to look like this:
[ 0 | 0 | 0 0 0 ]
The O's give the value in unary, so this combination is 1 1 3. To recover the values easily, we just augment the combinations with imaginary partitions at the next values to the left and right of the array (0 and n+k) and take the difference and subtract 1 (because the partitions themselves don't contribute to the value):
diff([0 2 4 8]) - 1
ans =
1 1 3
By sliding the partitions in to each possible combination of positions, we get all of the partitions of n.
Output:
result =
0 0 5
0 1 4
0 2 3
0 3 2
0 4 1
0 5 0
1 0 4
1 1 3
1 2 2
1 3 1
1 4 0
2 0 3
2 1 2
2 2 1
2 3 0
3 0 2
3 1 1
3 2 0
4 0 1
4 1 0
5 0 0

Is there a fast way to count occurrences of items in a matrix and save them in another matrix without using loops?

I have a time-series matrix X whose first column contains user ID and second column contains the item ID they used at different times:
X=[1 4
2 1
4 2
2 3
3 4
1 1
4 2
5 3
2 1
4 2
5 4];
I want to find out which user used which item how many times, and save it in a matrix Y. The rows of Y represent users in ascending order of ID, and the columns represent items in ascending order of ID:
Y=[1 0 0 1
2 0 1 0
0 0 0 1
0 3 0 0
0 0 1 1]
The code I use to find matrix Y uses 2 for loops which is unwieldy for my large data:
no_of_users = size(unique(X(:,1)),1);
no_of_items = size(unique(X(:,2)),1);
users=unique(X(:,1));
Y=zeros(no_of_users,no_of_items);
for a=1:size(A,1)
for b=1:no_of_users
if X(a,1)==users(b,1)
Y(b,X(a,2)) = Y(b,X(a,2)) + 1;
end
end
end
Is there a more time efficient way to do it?
sparse creates a sparse matrix from row/column indices, conveniently accumulating the number of occurrences if you give a scalar value of 1. Just convert to a full matrix.
Y = full(sparse(X(:,1), X(:,2), 1))
Y =
1 0 0 1
2 0 1 0
0 0 0 1
0 3 0 0
0 0 1 1
But it's probably quicker to just use accumarray as suggested in the comments:
>> Y2 = accumarray(X, 1)
Y2 =
1 0 0 1
2 0 1 0
0 0 0 1
0 3 0 0
0 0 1 1
(In Octave, sparse seems to take about 50% longer than accumarray.)

Matlab How to assign values on non-diagonal matrix

I have a matrix called "C" with 5x5 dimension, and i want to put 3 formulas (first formula where I have 0 (on diagonal, the second one where I have 1 and the third one where I have 2)
0 1 1 1 1
2 0 1 1 1
2 2 0 1 1
2 2 2 0 1
2 2 2 2 0
The above notation is just to know where I want to put the formula.
For diagonal it works with:
C(logical(eye(size(C)))) = V
where V is my formula.

Selecting that pixel that minimizes the distance

Say I have the following two matrices:
>> x = [1 4 3; 6 4 3; 6 9 3; 2 4 3; 5 4 0; 5 3 1; 6 4 7];
>> y = [0 0 1; 1 1 0; 1 1 0; 0 1 1; 0.2 0.8 0.54; 1 1 1; 0 0 0];
Where you can think of x as some image, and y as the degree of membership of each element of x to some region of interest.
Say I set those elements in x that have degree of membership = 1 to 1 (core) and the other elements to 0 as follows:
x = zeros(size(y));
x(y==1) = 1;
In which case I will have the following output:
0 0 1
1 1 0
1 1 0
0 1 1
0 0 0
1 1 1
0 0 0
Now, for the elements of 0, I substitute their values with the value of y in the corresponding location as follows:
x(x==0)=y(x==0);
Now, I select those pixels that are considered 4-neighbours of core but not in core as follows:
four_neighbourhood_pixels = imdilate(core, strel('diamond', 1)) - core;
My question is: how can we select a pixel p that belongs to four_neighbourhood_pixels that minimizes the distance between x & core?
Provided that for distance I calculate it as follows:
pdist([x,core],'minkowski');
Provided that x in the preceding command will be the matrix after substituting the zeros with the degree of membership values y i the corresponding location?
So, how can I select that pixel that belongs to four_neighbourhood_pixels that minimizes the distance between x with the zeros substituted and core?
Thanks.
If I understand correctly, core is the following matrix:
0 0 1
1 1 0
1 1 0
0 1 1
0 0 0
1 1 1
0 0 0
First find the distance between x and core.
dist=pdist([x,core],'minkowski');
dist1=squareform(dist);
[row1,row2]=find(dist1==min(dist1(:)); %interpretation: you get the minimum distance between row1 and row2 of [x core]
Veify if my understanding is correct:
You want a pixel from x which minimizes the distance dist and it should belong to four_neighbourhood_pixels. This is the matrix [x core]
1 4 3 0 0 1
6 4 3 1 1 0
6 9 3 1 1 0
2 4 3 0 1 1
5 4 0 0 0 0
5 3 1 1 1 1
6 4 7 0 0 0
Suppose you get the minimum value between 2nd row and 3rd row. Now based on this tell us what you mean by "find a pixel which minimizes..."