I can create a graph using the matlab functions but I need to create an adjacency matrix from the graph now.
Here is how I created the graph:
s = roadmap.edges(:,1); % vector [1, 2, 3, 4, ...]
t = roadmap.edges(:,2); % vector [1, 2, 3, 4, ...]
weights = roadmap.edge_lengths'; % vector [1, 2, 3, 4, ...]
G = graph(s, t, weights);
I need to create a adjacency matrix from this data and I want it to look like this:
G = [0 3 9 0 0 0 0;
0 0 0 7 1 0 0;
0 2 0 7 0 0 0;
0 0 0 0 0 2 8;
0 0 4 5 0 9 0;
0 0 0 0 0 0 4;
0 0 0 0 0 0 0;
];
Related
I have a cell array where each cell contains different size of square adjacency matrix(in MATLAB) for example
A = 29x29 double
30x30 double
24x24 double
10x10 double
Now I want to create One Block diagonal matrix B from each cell of A such that each cell Of A is in the diagonal in B. Example
B = [29X29] 0 0 0 0 0 0 0 0 0 0
0 0 [30x30] 0 0 0 0 0 0 0
0 0 0 0 [24X24] 0 0 0
0 0 0 0 0 0 [10x10]
so B would NxN where N = 29+30+24+10
I tried with the following code but it did not work.
function B =blockD(A)
n=size(A,1);
for i = 1:n
B=blkdiag(A{i});
end
end
Also at the end I have to row normalised matrix B
Just use B = blkdiag(A{:})
octave:4> A(1) = [1,2; 3, 4]
A = {3x1x3 Cell Array}
octave:5> A(2) = [1,2; 6, 4]
A = {3x1x3 Cell Array}
octave:6> A(3) = [1, 7; 5 8]
A = {3x1x3 Cell Array}
octave:7> B = blkdiag(A{:})
B =
1 2 0 0 0 0
3 4 0 0 0 0
0 0 1 2 0 0
0 0 6 4 0 0
0 0 0 0 1 7
0 0 0 0 5 8
Assume you have an 4x4 matrix A of zeros:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
And an 4x1 vector B that represents column indices for matrix A (so values [1:4])
4
2
3
1
Now I want to increment those columnpositions in matrix A on the index on every row from vector B.
I have tried a couple of constructions myself but can't quite manage to do this.
For example I tried:
A(:, B) = A(:, B)+1
Which just increment every element in A.
This is how I want the operation to act:
>> A(somethting(B)) = A(somethting(B)) + 1
0 0 0 1
0 1 0 0
0 0 1 0
1 0 0 0
You can do this by using the linear index to each of the elements you want to address. Compute this using sub2ind:
>> A = zeros(4)
A =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> B = [4 2 3 1]
B =
4 2 3 1
>> i=sub2ind(size(A),B,1:4)
i =
4 6 11 13
>> A(i) = A(i)+1
A =
0 0 0 1
0 1 0 0
0 0 1 0
1 0 0 0
Well just in case you want a looped version :p
A = zeros(4,4);
B = [4, 2, 3, 1];
for i = 1:length(B)
A(i, B(i) ) = A(i, B(i) ) + 1;
end
A = zeros(4);
B = [4 2 3 1];
A(repmat([1:4]',1,4) == repmat(B,4,1)) = 1
A =
0 0 0 1
0 1 0 0
0 0 1 0
1 0 0 0
Consider an index vector consisting of ones and zeros:
I=[0 0 1 0 1 1 0 0 0];
How can I easily generate the following matrix in matlab:
J=[0 2;
1 1;
0 1;
1 2;
0 3];
Use diff:
I = [0 0 1 0 1 1 0 0 0];
d = diff(I);
ind = [1 find(d~=0)+1]; %// starting index of each new value
rep = diff([ind numel(I)+1]); %// number of repetitions of each new value
J = [ I(ind).' rep.' ];
Using strfind for a slightly bigger example -
I =[1 1 0 0 1 0 1 1 0 0 0 1 1 1 1 0 0]
zero_pos = ['0' num2str(bsxfun(#eq,I,0),'%1d') '0']
ind3 = [ strfind(zero_pos,'01') ; strfind(zero_pos,'10')]
counts = diff(ind3(:))
var = zeros(numel(counts),1);
var(2:2:end)=1;
J = [var counts];
if ind3(1,1)-1>0
J = [1 ind3(1,1)-1;J];
end
Output
J =
1 2
0 2
1 1
0 1
1 2
0 3
1 4
0 2
Let us say that we have a matrix A1 and two vectors v1 and v2 as follow:
A1=zeros(5, 5);
v1=[1 2 3];
v2=[5 5 4];
Is there a way to replace the elements of A1 using v1 and v2 as indices one by one? i.e., insert in A1(1, 5), A1(2, 5), and in A1(3, 4) some elements.
The following do all combinations of v1 and v2. I want only one by one. i.e., v1(1) with v2(1), v1(2) with v2(2), and so on.
A1(v1, v2)
Basically you have row and column information and need to convert them into a linear index, to index into A1. For this, use sub2ind -
A1(sub2ind(size(A1),v1(1),v2(1))) = 12
A1(sub2ind(size(A1),v1(2),v2(2))) = 10
A1(sub2ind(size(A1),v1(3),v2(3))) = 9
Output -
A1 =
0 0 0 0 12
0 0 0 0 10
0 0 0 9 0
0 0 0 0 0
0 0 0 0 0
If you have those values stored in some array, array1, use this for the same result as above -
array1 = [12 10 9];
A1(sub2ind(size(A1),v1,v2)) = array1;
Convert your vectors into linear indices:
A1=zeros(5, 5);
v1=[1 2 3];
v2=[5 5 4];
ind=sub2ind(size(A1), v1, v2);
A1(ind(1))=1
A1 =
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
etc.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
guys I need to convert this data of the array :X=[1 5, 2 4, 2 5] using Matlab to this matrix :
[1 0 0 0 0]
[0 1 0 0 0]
[0 0 0 0 0]
[0 1 0 0 0]
[1 1 0 0 0]
where [1 5] are on the same column ,same for the rest [2 4,2 5].The size of the matrix [n n] is the maximum number in the array X, in which 5;
any suggestions? Thank you in advance.
Find n
X = [1 5; 2 4; 2 5];
n = max( X(:) );
M = zeros( n ); % n-by-n matrix
M( sub2ind( [n n], X(:,2), X(:,1) ) ) = 1
M =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
1 1 0 0 0
Edit It is best to represent incidence / adjacency matrix of a graph using sparse matrix:
M = accumarray( {[X(:,2); X(:,1)], [X(:,1);X(:,1)]}, 1, [ n n], #(x) 1, 0, true )
M =
(1,1) 1
(5,1) 1
(2,2) 1
(4,2) 1
(5,2) 1
And
full(M) =
1 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 1 0 0 0
1 1 0 0 0