MATLAB: All possible combinations of binary matrices - matlab

I am looking to find all possible linear combinations of a set of matrices over GF(2). I know the number of matrices, k, and they are all the same dimension, stored in a 3D array, C(:,:,i) for the ith matrix. Because I'm working over GF(2), all the coefficients of the linear combination must be in {0,1}. I would like to generate each of the 2^k possible sums so that I may test the resulting matrix for a required property. There are many posts about generating all combinations of elements of matrices or vectors, but I am looking to generate all linear combinations of the matrices as a whole.
Many Thanks!

Here is an example:
%# some data to work with
sz = [4 3];
k = 6;
C = rand([sz k]);
%# coefficients [0,0,0,0,0,0] to [1,1,1,1,1,1]
p = (dec2bin(0:2^k-1) == '1');
%# generate all linear combinations with the above coefficients
for i=1:size(p,1)
%# C(:,:,1)*p(i,1) + C(:,:,2)*p(i,2) + ... + C(:,:,k)*p(i,k)
linComb = sum(bsxfun(#times, permute(p(i,:),[1 3 2]), C),3);
%# do something interesting with it ...
end

Related

Matlab code for generating a particular class of matrices

I need to generate all square matrices of order n with given properties.
Matrices are symmetric.
Entries are 0 and 1.
Diagonal elements are zeros.
I am using Matlab2012b. Can you help me with the code?
I was trying to write it down. It needs a long sequences of for loops. Any simpler technique?
Try this:
N = 4; %// matrix size
M = (N^2-N)/2; %// number of values to fill in each matrix
P = 2^M; %// number of matrices
x = dec2bin(0:P-1)-'0'; %// each row contains the values of a matrix, "packed" in a vector
result = NaN(N,N,P); %// preallocate
for k = 1:P
result(:,:,k) = squareform(x(k,:)); %// unpack values
end
The matrices are result(:,:,1), result(:,:,2) etc.

Multiply two matrices in Matlab to obtain 3-dimensional matrix

I have two sparse matrices in Matlab, A and B,
and I want to compute a three-dimensional matrix C such that
C(i,j,k) = A(i,j) * B(j,k)
can I do this without a loop?
(Side question: Is there a name for this operation?)
Edit:
Seems my question has already been asked (just for full matrices):
Create a 3-dim matrix from two 2-dim matrices
For full matrices:
You can do it using bsxfun and shiftdim:
C = bsxfun(#times, A, shiftdim(B,-1))
Explanation: Let A be of size M x N and B of size N x P. Applying shiftdim(B,-1) gives a 1 x N x P array. bsxfun implicitly replicates A along the third dimension and shiftdim(B,-1) along the first to compute the desired element-wise product.
Another possibility, usually less efficient than bsxfun, is to repeat the arrays explicity along the desired dimensions, using repmat:
C = repmat(A, [1 1 size(B,2)]) .* repmat(shiftdim(B,-1), [size(A,1) 1 1])
For sparse matrices:
The result cannot be sparse, as sparse ND-arrays are not supported.. But you can do the computations with sparse A and B using linear indexing:
ind1 = repmat(1:numel(A),1,size(B,2));
ind2 = repmat(1:numel(B),size(A,1),1);
ind2 = ind2(:).';
C = NaN([size(A,1),size(A,2),size(B,2)]); %// preallocate with appropriate shape
C(:) = full(A(ind1).*B(ind2)); %// need to use full if C is to be 3D
Answer to your side question: the name for this operation is a hash join.

How to multiply two vectors with different length

Let's say I have two vectors:
A = [1 2 3];
B = [1 2];
And that I need a function similar to multiplication of A*B to produce the following output:
[
1 2 3
2 4 6
]
It seems that things like A*B, A*B' or A.*B are not allowed as the number of elements is not the same.
The only way I managed to do this (I am quite new at MATLAB) is using ndgrid to make two matrices with the same number of elements like this:
[B1,A1] = ndgrid(B, A);
B1.*A1
ans =
1 2 3
2 4 6
Would this have good performance if number of elements was large?
Is there a better way to do this in MATLAB?
Actually I am trying to solve the following problem with MATLAB:
t = [1 2 3]
y(t) = sigma(i=1;n=2;expression=pi*t*i)
Nevertheless, even if there is a better way to solve the actual problem in place, it would be interesting to know the answer to my first question.
You are talking about an outer product. If A and B are both row vectors, then you can use:
A'*B
If they are both column vectors, then you can use
A*B'
The * operator in matlab represents matrix multiplication. The most basic rule of matrix multiplication is that the number of columns of the first matrix must match the number of rows of the second. Let's say that I have two matrices, A and B, with dimensions MxN and UxV respectively. Then I can only perform matrix multiplication under the following conditions:
A = rand(M,N);
B = rand(U,V);
A*B % Only valid if N==U (result is a MxV matrix)
A'*B % Only valid if M==U (result is a NxV matrix)
A*B' % Only valid if N==V (result is a MxU matrix)
A'*B' % Only valid if V==M (result is a UxN matrix)
There are four more possible cases, but they are just the transpose of the cases shown. Now, since vectors are just a matrix with only one non-singleton dimension, the same rules apply
A = [1 2 3]; % (A is a 1x3 matrix)
B = [1 2]; % (B is a 1x2 matrix)
A*B % Not valid!
A'*B % Valid. (result is a 3x2 matrix)
A*B' % Not valid!
A'*B' % Not valid!
Again, there are four other possible cases, but the only one that is valid is B'*A which is the transpose of A'*B and results in a 2x3 matrix.

MATLAB accessing multiple elements in sparse matrix using row and column index vectors

I feel there should be an easy solution but I can't find it:
I have the sparse matrices A B with the same dimension n*n. I want to create matrix C which copies values in A where B is non-zero.
This is my approach:
[r,c,v] = find(B);
% now I'd like to create an array of values using indices r and c,
% but this doesn't work (wrong syntax)
v2 = A(r,c);
% This won't work either
idx = find(B); % linear indexing, too high-dimensional
v2 = A(idx);
% and create C
C = sparse(r,c,v2,n,n);
Here are some more details:
My matrices are very large, so the solution needs to be efficient. C(B~=0) = B(B~=0); won't do it, unfortunately.
Linear indexing won't work either as the matrices are too large (Matrix is too large to return linear indices.).
Is there really no way to use 2-dimensional indices?
Thanks for your help!
I think C = A .* (B~=0); should work. Only non-zeros will be accessed in the entrywise multiplication of two sparse matrices so it will be fast.

Random permutation matrix

Is there an easy way to simulate a random permutation matrix (say of size 1000 by 1000) in Matlab? I would like to study the eigenvalue distribution of independent sum of such matrices.
Thanks in advance!
You can generate a random permutation matrix like so:
Create a unity matrix:
A = eye( N ); %// N is the size of your matrix
For large values of N it is better to use sparse matrices:
A = speye( N ); % create sparse identity matrix
Generate a random permutation:
idx = randperm(1:N);
Use vector indexing to rearrange the rows accordingly
A = A(idx, :);
Voila!
In Matlab (used R2012a) idx = randperm(1:N) gives a warning that input should be scalar. So: idx = randperm(N); .