Extracting multiple Sub-matrices of different sizes from Large Matrices
조회 수: 5 (최근 30일)
이전 댓글 표시
I have a set of 3 large matrices
A = [
1 3 0 0 0 0 1 1 0 345 1 1.1 0.9;
2 2 0 0 0 0 1 1 0 345 1 1.1 0.9;
3 2 0 0 0 0 1 1 0 345 1 1.1 0.9;
4 1 0 0 0 0 1 1 0 345 1 1.1 0.9;
5 1 90 30 0 0 1 1 0 345 1 1.1 0.9;
6 1 0 0 0 0 1 1 0 345 1 1.1 0.9;
7 1 100 35 0 0 1 1 0 345 1 1.1 0.9;
8 1 0 0 0 0 1 1 0 345 1 1.1 0.9;
9 1 125 50 0 0 1 1 0 345 1 1.1 0.9;];
%
B = [
1 0 0 300 -300 1 100 1 250 10 0 0 0 0 0 0 0 0 0 0 0;
2 163 0 300 -300 1 100 1 300 10 0 0 0 0 0 0 0 0 0 0 0;
3 85 0 300 -300 1 100 1 270 10 0 0 0 0 0 0 0 0 0 0 0;];
C = [
1 4 0 0.0576 0 250 250 250 0 0 1 -360 360;
4 5 0.017 0.092 0.158 250 250 250 0 0 1 -360 360;
5 6 0.039 0.17 0.358 150 150 150 0 0 1 -360 360;
3 6 0 0.0586 0 300 300 300 0 0 1 -360 360;
6 7 0.0119 0.1008 0.209 150 150 150 0 0 1 -360 360;
7 8 0.0085 0.072 0.149 250 250 250 0 0 1 -360 360;
8 2 0 0.0625 0 250 250 250 0 0 1 -360 360;
8 9 0.032 0.161 0.306 250 250 250 0 0 1 -360 360;
9 4 0.01 0.085 0.176 250 250 250 0 0 1 -360 360; ];
Part A The first and second columns of matrix C correspond to starting and target nodes of a graph respectively. I have created a graph with 9 nodes. The reference node in this case is 1. I need to determine the shortest path between 1 and each of the nodes in the graph, then determine all other nodes connected to each node by a a single edge. That is Let G be the graph, let i denote each of the nodes that is i={1, ..., 9}. For each i, create a set of nodes which includes 1) all nodes in the shortest path between node 1 and node i 2) All nodes directly connected to i but not on the shortest path from 1, that is exactly one hop away from i or Immediately adjacent to i. This is the MATLAB code I have written for this
Edges=adjacency(G);
Path = shortestpath(G,1,5);
node = graphtraverse(Edges,5,'depth',1);
seti=unique([Path node]);
Answer:
seti=[1 4 5 6]
This is for just one node (i=5). I want to do this for all 9 nodes simultaneously
Part B After creating the set seti for i={1, ...., 9} with the list of adjacent nodes and nodes on the shortest path for i. From the matrices A and B. I would like to search the first column for numbers corresponding to any of the numbers within the node set. When matching numbers are found, I would like to extract the entire row in A and B corresponding to the row in which a matching element has been found. In other words for all 9 node sets created above, I would like to create smaller 9 submatrices from the A and B large matrices.
This MATLAB code enables me extract submatrices for only node 5
[ii5,jj5] = find(squeeze(any(A(:,1) == reshape([seti],1,1,[]),2)));
A5 = accumarray(jj5,ii5,[],@(x){A(x,:)});
A5=cell2mat(A5);
Answer:
A5=[ 1 3 0 0 0 0 1 1 0 345 1 1.1 0.9;
4 1 0 0 0 0 1 1 0 345 1 1.1 0.9;
5 1 90 30 0 0 1 1 0 345 1 1.1 0.9;
6 1 0 0 0 0 1 1 0 345 1 1.1 0.9; ];
%Submatrix from B
[ii5a,jj5a] = find(squeeze(any(B(:,1) == reshape([seti],1,1,[]),2)));
B5 = accumarray(jj5a,ii5a,[],@(x){B(x,:)});
B5=cell2mat(B5);
Answer:
B5= [1 0 0 300 -300 1 100 1 250 10 0 0 0 0 0 0
0 0 0 0];
Part C Now I want to extract submatrices from the matrix C. I need to search the first two columns of the matrix 'C' and extract submatrices again based on the set of nodes in set i above. This time I start by searching for i from the first 2 columns of C if node i is found in either the first or second column, I extract the entire corresponding row then generate a submatrix. I again search the same two columns for the rest of the node ids in the seti, I need to extract an entire row if both the elements in the first and second column of that row are in the original set of nodes seti. I then create one submatrix from these two submatrices and remove duplicates This is how am extracting the submatrix where the first or second columns contain i.
i=5
[ii5b,jj5b] = find(squeeze(any(C(:,1:2) == reshape([5],1,1,[]),2)));
C5a = accumarray(jj5b,ii5b,[],@(x){C(x,:)});
C5a=cell2mat(C5a)
For i=5
submatrix 1 in part c would be
C5a=[4 5 0.017 0.092 0.158 250 250 250 0 0 1 -360 360;
5 6 0.039 0.17 0.358 150 150 150 0 0 1 -360 360;]
submatrix 2 should be C5B=[1 4 0 0.0576 0 250 250 250 0 0 1 -360 360;] Desired matrix
C5=[4 5 0.017 0.092 0.158 250 250 250 0 0 1 -360 360;
5 6 0.039 0.17 0.358 150 150 150 0 0 1 -360 360;
1 4 0 0.0576 0 250 250 250 0 0 1 -360 360;]
Questions How can extract submatrix 2 for part c? s in from matrix C, How can I extract a submatrix by searching the first two columns for only those rows in which both the first and second element in col 1 and 2 are in seti.
Like I mentioned earlier my examples are for only one node (i=5). I need to do this for all 9 nodes simultaneous, I would like to generate all 9 sub-matrices for each Part A Part B and Part C. As in a total of 27 matrices simultaneously.
Please advise on a good way to this?
The reason for this is I am going to be changing values in the main matrices A, B, C like 1000 times and I want these changes to propagate to the submatrices simultaneously I'd appreciate any input.
Thank you, puttogether
댓글 수: 0
답변 (1개)
Faiz Gouri
2017년 8월 16일
You can use 'ismember' function to compare first two columns of 'C' and 'seti' and then extract rows from C where they are same.
result = C(ismember(C(:,1:2),seti(:,1:2), 'rows'), :)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!