Element-wise concatenation of 2 vectors in kdb+ - kdb

Given 2 vectors A and B, I want to concatenate each element of A with each element of B. For example if A and B were as follows:
A: 0 1 2
B: 3 4 5
then the output should be (0 3;1 4; 2 5)

Joining the two vectors using the each (each-both in this case) iterator returns your desired output.
q)0N!A,'B
(0 3;1 4;2 5)
0 3
1 4
2 5

You could also instantiate through the following
(A;B) to create a 2x3 matrix which can be flipped to get what you require
q)A:0 1 2
q)B:3 4 5
q)(A;B)
0 1 2
3 4 5
q)flip (A;B)
0 3
1 4
2 5

Related

Nested loop and conditional statement (Matlab)

If you have a random matrix, for example a 5x5:
A(i,j) = (5 4 3 2 1
4 3 2 1 0
5 4 3 2 1
4 3 2 1 0
5 4 3 2 1)
And a second array:
B(1,j) = (4 5 6 7 8)
How can I then assign values of B to A if this only needs to be done when the value of B(1,j) is larger than any of the values from a certain colomn of A?
For example, B(1,1) = 4 and in the first colomn of A it is larger than A(1,1), A(3,1) and A(5,1), so these must be replaced by 4. In the second colomn, nothing needs to be replaced, etc.
Thanks already!
You can do this without any explicit looping using bsxfun:
A = [5 4 3 2 1
4 3 2 1 0
5 4 3 2 1
4 3 2 1 0
5 4 3 2 1];
B = [4 5 6 7 8];
A = bsxfun(#min,A,B);
Result:
A =
4 4 3 2 1
4 3 2 1 0
4 4 3 2 1
4 3 2 1 0
4 4 3 2 1
In later versions of MATLAB (2016b and later) you can even omit the bsxfun and get the same result.
A = min(A,B);
Matlab "find" may be of use to you.
https://www.mathworks.com/help/matlab/matlab_prog/find-array-elements-that-meet-a-condition.html
If you aren't concerned about speed or efficiency, you could also set up a two nested for loops with a condition (i.e. an if) statement comparing the values of A and B.
If you only interested in column wise comparison to B, you could use the increment of the outer loop in the inner loop.
for i,...
for j,...
if B(1,i) > A(j,i)
A(j,i)=B(i,j)

Straighten and concatenate the individual grids from ndgrid [duplicate]

This question already has answers here:
Generate a matrix containing all combinations of elements taken from n vectors
(4 answers)
Closed 6 years ago.
I'm trying to do the following in a general way:
x = {0:1, 2:3, 4:6};
[a,b,c] = ndgrid(x{:});
Res = [a(:), b(:), c(:)]
Res =
0 2 4
1 2 4
0 3 4
1 3 4
0 2 5
1 2 5
0 3 5
1 3 5
0 2 6
1 2 6
0 3 6
1 3 6
I believe I have to start the following way, but I can't figure out how to continue:
cell_grid = cell(1,numel(x));
[cell_grid{:}] = ndgrid(x{:});
[cell_grid{:}]
ans =
ans(:,:,1) =
0 0 2 3 4 4
1 1 2 3 4 4
ans(:,:,2) =
0 0 2 3 5 5
1 1 2 3 5 5
ans(:,:,3) =
0 0 2 3 6 6
1 1 2 3 6 6
I can solve this in many ways for the case with three variables [a, b, c], both with and without loops, but I start to struggle when I get more vectors. Reshaping it directly will not give the correct result, and mixing reshape with permute becomes really hard when I have arbitrary number of dimensions.
Can you think of a clever way to do this that scales to 3-30 vectors in x?
You can use cellfun to flatten each of the cell array elements and then concatenate them along the second dimension.
tmp = cellfun(#(x)x(:), cell_grid, 'uniformoutput', false);
out = cat(2, tmp{:})
Alternately, you could avoid cellfun and concatenate them along the dimension that is one higher than your dimension of each cell_grid member (i.e. numel(x) + 1). Then reshape to flatten all dimensions but the last one you just concatenated along.
out = reshape(cat(numel(x) + 1, cell_grid{:}), [], numel(x));

Creating an index matrix depending on Reference matrix and matrix of Data matlab

given matrix A of size 6 by 6 contain blocks of numbers,each block of size 2 by 2, and outher reference matrix R of size 2 by 12 also contain blocks of numbers, each block of size 2 by 2. the perpse of the whole process is to form a new matrix, called the Index matrix, contain index's that refer to the position of the blocks within the matrix A based on the order of the blocks within the reference matrix R. and here is an exemple
matrix A:
A =[1 1 2 2 3 3;
1 1 2 2 3 3;
1 1 3 3 4 4;
1 1 3 3 4 4;
4 4 5 5 6 6;
4 4 5 5 6 6 ]
matrix R:
R=[1 1 2 2 3 3 4 4 5 5 6 6;
1 1 2 2 3 3 4 4 5 5 6 6 ]
the new matrix is:
Index =[1 2 3;
1 3 4;
4 5 6]
any ideas ?
With my favourite three guys - bsxfun, permute, reshape for an efficient and generic solution -
blksz = 2; %// blocksize
num_rowblksA = size(A,1)/blksz; %// number of blocks along rows in A
%// Create blksz x blksz sized blocks for A and B
A1 = reshape(permute(reshape(A,blksz,num_rowblksA,[]),[1 3 2]),blksz^2,[])
R1 = reshape(R,blksz^2,1,[])
%// Find the matches with "bsxfun(#eq" and corresponding indices
[valid,idx] = max(all(bsxfun(#eq,A1,R1),1),[],3)
%// Or with PDIST2:
%// [valid,idx] = max(pdist2(A1.',reshape(R,blksz^2,[]).')==0,[],2)
idx(~valid) = 0
%// Reshape the indices to the shapes of blocked shapes in A
Index = reshape(idx,[],num_rowblksA).'
Sample run with more random inputs -
>> A
A =
2 1 1 2
1 2 2 1
1 1 1 1
2 2 2 2
1 2 2 1
1 2 1 1
>> R
R =
2 1 1 1 1 2 2 2 1 1 1 1
2 1 2 1 1 2 2 1 2 2 2 1
>> Index
Index =
0 0
5 5
3 0

Find column vectors contained in B but not in A - MATLAB

I have matrix A of size 10x100 and matrix B with size 10x200.
How to find the column vectors contained in B but not in A? ( A and B do not have the same number of columns)
to elaborate #Cheery's comment with an example.
A=[1;4];
B=[1 2 4;4 5 6];
C=setdiff(B',A','rows')';
more details see http://www.mathworks.com/help/matlab/ref/setdiff.html
You can also use bsxfun here -
Bout = B(:,all(any(bsxfun(#ne,B,permute(A,[1 3 2])),1),3))
Sample run -
A =
2 2 2 2 2
2 2 1 1 1
1 1 2 1 3
B =
3 2 3 2 1 2 2
3 1 3 1 1 1 3
2 3 1 2 1 2 3
Bout =
3 3 1 2
3 3 1 3
2 1 1 3

matlab adjacency list to adjacency matrix

How to convert adjacency list to adjacency matrix via matab
For example: Here is the adjacency list(undirected), the third column is the weight.
1 2 3
1 3 4
1 4 5
2 3 4
2 5 8
2 4 7
++++++++++++++++++++++
that should be converted to:
1 2 3 4 5
1 0 4 5 0
2 3 4 7 8
3 4 7 0 0
4 0 7 0 0
5 0 8 0 0
You can use sparse matrix. Let rows be the first column, cols the second, and s the weight.
A = sparse([rows; cols],[cols; rows],[s; s]);
If you want to see the matrix. use full().
UPDATE:
I made the answer a bit simpler (everything in one line, instead of adding the transposed, and included explanations, as requested:
list = [1 2 3
1 3 4
1 4 5
2 3 4
2 5 8
2 4 7];
rows = list(:,1)
cols = list(:,2)
s = list(:,3)
Now, rows, cols and s contains the needed information. Sparse matrices need three vectors. Each row of the two first vectors, rows and cols is the index of the value given in the same row of s (which is the weight).
The sparse command assigns the value s(k) to the matrix element adj_mat(rows(k),cols(k)).
Since an adjacency matrix is symmetric, A(row,col) = A(col,row). Instead of doing [rows; cols], it is possible to first create the upper triangular matrix, and then add the transposed matrix to complete the symmetric matrix.
A = sparse([rows; cols],[cols; rows],[s; s]);
full(A)
A =
0 3 4 5 0
3 0 4 7 8
4 4 0 0 0
5 7 0 0 0
0 8 0 0 0
It's really hard to tell what your'e asking. Is this right?
list = [1 2 3
1 3 4
1 4 5
2 3 4
2 5 8
2 4 7];
matrix = zeros(max(max(list(:, 1:2)))); %// Or just zeros(5) if you know you want a 5x5 result
matrix(sub2ind(size(matrix), list(:,1), list(:,2))) = list(:,3); %// Populate the upper half
matrix = matrix + matrix' %'// Find the lower half via symmetry
matrix =
0 3 4 5 0
3 0 4 7 8
4 4 0 0 0
5 7 0 0 0
0 8 0 0 0