solving binary linear equation in matlab - matlab

Consider a binary linear equation of form x*A = b. I want to solve for x, the efficiency way is by avoiding using x = b*inv(A) and instead using x= b/A.But with this command results are not in binary form. I tried the command x = mod(b/A ,2)but still result was not in binary. How to fix this?
example
`
x = 1 0 1 1 0 0 0 1 1 0`
and matrix A is
`0 1 0 1 0 0 1 1 1 1
0 1 1 1 1 1 1 1 0 1
0 0 1 0 1 1 0 1 1 1
1 0 0 1 1 1 1 1 1 1
1 1 0 0 1 1 0 0 0 0
0 1 1 1 0 1 1 0 1 0
0 0 1 1 0 0 0 1 0 0
1 1 1 1 0 1 0 1 1 1
1 0 1 0 1 1 1 0 1 1
1 1 1 0 0 0 1 1 0 0`
which is full rank.
then
>> b = mod (x*A,2)
b =
1 0 1 1 1 0 1 0 1 1
To find x, am getting
>> k = b / A
k =
Columns 1 through 6
1.3750 -0.5000 -0.7500 -0.7500 0.8750 -0.5000
Columns 7 through 10
1.8750 -0.5000 2.1250 -0.7500
or if am using modulus 2, the result is
>> k = mod (b / A,2)
k =
Columns 1 through 6
1.3750 1.5000 1.2500 1.2500 0.8750 1.5000
Columns 7 through 10
1.8750 1.5000 0.1250 1.2500
So ,how can I get x in the same binary form? and by the way matrices are all in class double not galois field

This example at Mathworks shows how to perform boolean matrix inversion with MATLAB and I believe answers your question.
I have not quite gotten it working perfectly, but I believe you need to use a combination of mod() and logical() for example:
A=logical(A);
b=(mod(x*A,2));
inverseA= ~A;
k=mod(b*inverseA,2)
this gives
k=[1 1 0 0 1 1 0 1 0 0]
which is not x, but i think if you play around with the logical function and logical operations in conjunction with mod() you should be able to get it to work

To solvexin binary form, matrix A should be in galois field.
consider the following example;
>> x = randi ([0 1],1,10)
x =
1 1 0 1 1 1 0 1 1 1
>> A = gf (randi([0 1],10,10))
A = GF(2) array.
Array elements =
Columns 1 through 6
1 1 1 0 0 1
1 0 1 0 1 0
1 0 1 1 0 1
0 0 0 0 0 1
1 1 1 0 1 1
0 1 0 1 0 1
0 0 0 1 1 1
0 1 0 0 1 0
0 0 1 0 1 0
0 0 1 0 0 1
Columns 7 through 10
1 1 0 0
1 0 1 1
1 1 0 0
0 0 1 0
1 1 1 1
0 1 1 1
1 0 1 0
1 1 1 0
1 0 0 0
1 1 1 0
then,
>> b = x*A
b = GF(2) array.
Array elements =
Columns 1 through 6
1 0 1 1 0 1
Columns 7 through 10
0 1 0 1
>> x_solved = b*inv (A)
x_solved = GF(2) array.
Array elements =
Columns 1 through 6
1 1 0 1 1 1
Columns 7 through 10
0 1 1 1
As you can see x_solved is the same as the original x. Therefore you should convert matrix A to galois field by just running a codeA = gf(A).

Related

Filter function for k elements larger than A

In R/Bioconductor's genefilter package, there is a nice function called kOverA (page 18 in this manual).
It's just a filter method that, given a numerical matrix, removes the rows of that matrix that do not have k-elements that are greater than or equal to A-value.
How can I do the same thing in MATLAB?
Examples (simplified. In R, kOverA returns a function, so the actual syntax is a bit different but this is the functionality that I want):
m = [1 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1];
kOverA(m, A=1, k=0) → m
kOverA(m, A=2, k=1) → empty
kOverA(m, A=1, k=1) → [1 0 0 0 0 0 1 1 1 0
1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1]
kOverA(m, A=1, k=4) → [1 0 0 0 0 0 1 1 1 0
1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1]
kOverA(m, A=1, k=5) → [1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1]
Requires relational operator >=, sum and logical indexing and this is it.
out = m(sum(m>=A,2) >= k,:);

Convert adjacency matrix to specific edge list in MATLAB

If I have the matrix
1 0 0
0 0 1
0 0 0
and I want this form in MATLAB
1 2 3 1 2 3 1 2 3
1 1 1 2 2 2 3 3 3
1 0 0 0 0 0 0 1 0
also I want the values of third row in result. i.e. ans= [1 0 0 0 0 0 0 1 0]
Here you go -
[X,Y] = ndgrid(1:size(A,1),1:size(A,2));
out = [X(:).' ; Y(:).' ; A(:).']
For the last part of your question, use the last row of out : out(end,:) or A(:).'.
Sample run -
>> A
A =
1 0 0
0 0 1
0 0 0
>> [X,Y] = ndgrid(1:size(A,1),1:size(A,2));
>> out = [X(:).' ; Y(:).' ; A(:).']
out =
1 2 3 1 2 3 1 2 3
1 1 1 2 2 2 3 3 3
1 0 0 0 0 0 0 1 0

Distance between connected components

I wanted to know if there is some inbuilt function to get distance between different connected components in MATLAB. I am using bwlabel to get the various connected components.Is there some way to get the distance between these connected components?
I guess you could use regionprops to locate the centroid of each connected component and then apply pdist to find the pairwise distance between each of them.
Simple example:
clear
clc
close all
%// Create logical array
BW = logical ([1 1 1 0 0 0 0 0
1 1 1 0 1 1 0 0
1 1 1 0 1 1 0 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 1 1 0
1 1 1 0 0 0 0 0])
%/ Call regionprops and concatenate centroid coordinates
S = regionprops(bwlabel(BW,4),'Centroid')
Centroids = vertcat(S.Centroid)
%// Measure pairwise distance
D = pdist(Centroids,'euclidean')
Outputs in the Command Window:
BW =
1 1 1 0 0 0 0 0
1 1 1 0 1 1 0 0
1 1 1 0 1 1 0 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 1 1 0
1 1 1 0 0 0 0 0
S =
3x1 struct array with fields:
Centroid
Centroids =
2.0000 4.5000
5.5000 2.5000
6.8000 5.8000
D =
4.0311 4.9729 3.5468

generating combinations in Matlab

I have a column vector x made up of 4 elements, how can i generate all the possible combinations of the values that x can take such that x*x' is less than or equal to a certain value?
note that the values of x are positive and integers.
To be more clear:
the input is the number of elements of the column vector x and the threshold, the output are the different possible combinations of the values of x respecting the fact that x*x' <=threshold
Example: threshold is 4 and x is a 4*1 column vector.....the output is x=[0 0 0 0].[0 0 0 1],[1 1 1 1]......
See if this works for you -
threshold = 4;
A = 0:threshold
A1 = allcomb(A,A,A,A)
%// Or use: A1 = combvec(A,A,A,A).' from Neural Network Toolbox
combs = A1(sum(A1.^2,2)<=threshold,:)
Please note that the code listed above uses allcomb from MATLAB File-exchange.
Output -
combs =
0 0 0 0
0 0 0 1
0 0 0 2
0 0 1 0
0 0 1 1
0 0 2 0
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
0 2 0 0
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
2 0 0 0

Assign values w/ multiple conditions

Let's have a M = [10 x 4 x 12] matrix. As example I take the M(:,:,4):
val(:,:,4) =
0 0 1 0
0 1 1 1
0 0 0 1
1 1 1 1
1 1 0 1
0 1 1 1
1 1 1 1
1 1 1 1
0 0 1 1
0 0 1 1
How can I obtain this:
val(:,:,4) =
0 0 3 0
0 2 2 2
0 0 0 4
1 1 1 1
1 1 0 1
0 2 2 2
1 1 1 1
1 1 1 1
0 0 3 3
0 0 3 3
If I have 1 in the first column then all the subsequent 1's should be 1.
If I have 0 in the first column but 1 in the second, all the subsequent 1's should be 2.
If I have 0 in the first and second column but 1 in the third then all the subsequent 1's should be 3.
If I have 0 in the first 3 columns but 1 in the forth then this one should be four.
Note: The logical matrix M is constructed:
Tab = [reshape(Avg_1step.',10,1,[]) reshape(Avg_2step.',10,1,[]) ...
reshape(Avg_4step.',10,1,[]) reshape(Avg_6step.',10,1,[])];
M = Tab>=repmat([20 40 60 80],10,1,size(Tab,3));
This is a very simple approach that works for both 2D and 3D matrices.
%// Find the column index of the first element in each "slice".
[~, idx] = max(val,[],2);
%// Multiply the column index with each row of the initial matrix
bsxfun(#times, val, idx);
This could be one approach -
%// Concatenate input array along dim3 to create a 2D array for easy work ahead
M2d = reshape(permute(M,[1 3 2]),size(M,1)*size(M,3),[]);
%// Find matches for each case, index into each matching row and
%// elementwise multiply all elements with the corresponding multiplying
%// factor of 2 or 3 or 4 and thus obtain the desired output but as 2D array
%// NOTE: Case 1 would not change any value, so it was skipped.
case2m = all(bsxfun(#eq,M2d(:,1:2),[0 1]),2);
M2d(case2m,:) = bsxfun(#times,M2d(case2m,:),2);
case3m = all(bsxfun(#eq,M2d(:,1:3),[0 0 1]),2);
M2d(case3m,:) = bsxfun(#times,M2d(case3m,:),3);
case4m = all(bsxfun(#eq,M2d(:,1:4),[0 0 0 1]),2);
M2d(case4m,:) = bsxfun(#times,M2d(case4m,:),4);
%// Cut the 2D array thus obtained at every size(a,1) to give us back a 3D
%// array version of the expected values
Mout = permute(reshape(M2d,size(M,1),size(M,3),[]),[1 3 2])
Code run with a random 6 x 4 x 2 sized input array -
M(:,:,1) =
1 1 0 1
1 0 1 1
1 0 0 1
0 0 1 1
1 0 0 0
1 0 1 1
M(:,:,2) =
0 1 0 1
1 1 0 0
1 1 0 0
0 0 1 1
0 0 0 1
0 0 1 0
Mout(:,:,1) =
1 1 0 1
1 0 1 1
1 0 0 1
0 0 3 3
1 0 0 0
1 0 1 1
Mout(:,:,2) =
0 2 0 2
1 1 0 0
1 1 0 0
0 0 3 3
0 0 0 4
0 0 3 0