How to represent given adjacency matrix as undirected weighted graph in matlab? - matlab

For below given matrix,how it can be represented as undirected weighted graph G(V,E,W) where V is set of vertices,E is set of edges and W is set of weights.
4 2 3 1 4
2 2 3 1 4
2 3 3 1 4
1 2 3 1 4
4 2 3 1 5
Source code i tried:
%table2 is given matrix
bg = biograph(table2,[],'ShowArrows','off','ShowWeights','on');
h = view(bg);
Is this the right way to represent undirected weighted graph for my matrix?Iam getting 2 edges between same 2 verteces.Just like this
I got this output for your suggested code
Iam having 2 adjacency matrices.I have to perform below 2 operations:
1.I have to make 2 graphs from these 2 adjacency matrices.
2.Then i have to match vertices of 2 graphs depending on weights of edges in graph.
4 2 3 1 4
2 2 3 1 4
2 3 3 1 4
1 2 3 1 4
4 2 3 1 5
4 1 3 2 4
1 1 2 3 4
3 1 3 2 4
2 1 3 2 4
4 1 3 2 5
The way iam going is right or wrong?Please suggest me

To get only one edge between each vertex, you only want a triangular matrix.
If you try something like this:
table1 = (table2 + table2') - triu((table2 + table2'))
table1 =
0 0 0 0 0
4 0 0 0 0
5 6 0 0 0
2 3 4 0 0
8 6 7 5 0
bg = biograph(table1,[],'ShowArrows','off','ShowWeights','on');
h = view(bg);
Now I have assumed you want to sum up the weights from both edges. I.e. the weight of the edge between 1-2 equal the weight of the edge from 1->2 + 2->1, from your original matrix.

Related

identify the adjacent pixels in matlab

Let's assume A be,
1 1 1 1 1 1
1 2 2 3 3 3
4 4 2 2 3 3
4 4 2 2 2 3
4 4 4 4 3 3
5 5 5 5 5 5
I need to identify all the numbers which are adjacent to a particular intensity value. E.g. the intensities 1, 3, and 4 are adjacent to the intensity value 2.
What is the effective way to do it in Matlab?
I can use the following,
glcm = graycomatrix(A)
But if A have a larger number of intensity values e.g. 10000 graycomatrix will not be an efficient method.
You can build a mask with a 2D convolution, select the values according to that mask, and then reduce them to unique values:
% // Data:
A = [ 1 1 1 1 1 1
1 2 2 3 3 3
4 4 2 2 3 3
4 4 2 2 2 3
4 4 4 4 3 3
5 5 5 5 5 5 ];
value = 2;
adj = [0 1 0; 1 0 1; 0 1 0]; %// define adjacency. [1 1 1;1 0 1;1 1 1] to include diagonals
%// Let's go
mask = conv2(double(A==value), adj, 'same')>0; %// pixels adjacent to those equal to `value`
result = unique(A(mask));
In the example, this produces
result =
1
2
3
4
Note that the result includes 2 because some pixels with value 2 have adjacent pixels with that value.

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

Loop through all combinations of 8 in 15 MATLAB

I have this matrix and want to make all combinations of column composed square matrixes (8x8) composed from this data.
4 2 4 3 2 3 3 2 8 4 9 7 6 6 6
2 0 4 1 0 3 0 8 5 0 9 3 7 7 1
2 1 2 1 1 3 1 4 5 2 4 2 6 6 3
0 0 2 2 1 2 3 9 1 1 4 4 4 4 6
4 0 1 0 4 2 3 1 8 1 3 0 5 5 7
3 1 4 0 0 1 0 2 6 2 9 1 2 2 0
1 2 1 4 0 3 4 1 3 4 3 9 7 7 9
2 0 0 4 0 0 3 1 5 0 1 9 1 1 7
Even after reeding Matlab Loop of all combinations
I'm not really sure how to do all the matrix combinations and include the counter from the for loop in the name of the combination obtained in the itteration.
I called your matrix A.
p=nchoosek(1:15,8);
gives all the combinations of 8 numbers taken from 1 to 15. These represent the columns of the matrix A that you want.
There are now 3 ways to proceed. Firstly, using a for loop:
M=zeros(8,8,size(p,1));
for i=1:size(p,1)
M(:,:,i)=A(:,p(i,:));
end
which puts each 8x8 matrix into a larger 3D array. You would get out individual matrices by doing M(:,:,54), for example.
You can also create a cell array:
N=arrayfun(#(k) A(:,p(k,:)),1:size(p,1),'UniformOutput',false);
and get individual matrices by doing N{54}.
Finally, you could not precompute each matrix, and just pull out the appropriate columns when you need them. This may be the most efficient method if you don't reuse the matrices:
O=A(:,p(54,:));

how to remove rows with same information but different representation?

given
A=1 1 0
1 2 0.563826213141399
1 3 1.18321595677734
1 4 1.95685972913029
2 1 0.563826213141399
2 2 0
2 3 0.830602192143995
2 4 1.65196852337589
2 5 1.77172232586001
3 1 1.18321595677734
3 2 0.830602192143995
3 3 0
3 4 0.821522975656861
3 5 1.12716458303105
3 6 1.78117938413852
as seen row 2 and 5 are same in real but not in the matrix. how can I remove one of the same rows?
using unique I couldn't do this.
It seems like you have a representation of a graph using a weighted adjacency matrix.
If I understand correctly, you wish to have a single entry per edge.
You can do this by taking only the upper triangle of the adjacency matrix
A = sparse( A(:,1), A(:,2), A(:,3), max(A(:,2)), max(A(:,2)) );
[ii jj wij] = find( triu( A ) ) ;
disp( [ii jj wij] )
outputs:
1 2 0.563826
1 3 1.183216
2 3 0.830602
1 4 1.956860
2 4 1.651969
3 4 0.821523
2 5 1.771722
3 5 1.127165
3 6 1.781179

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