Domanda

I have a sparse matrix:

 A=


  (14,13)      0.5286
  (15,14)      0.6781
  (16,15)      0.5683
  (17,16)      1.2773
  (18,17)      1.0502
  (19,18)      0.4966
  (21,19)      0.9951
  (21,20)      0.4522
  (22,21)      0.8507
  (23,22)      1.0727
  (24,23)      0.8288
  (25,24)      0.5811
  (26,25)      0.8235
  (28,26)      1.5128
  (30,28)      0.7966
  (30,29)      0.6363
  (31,29)      0.8254
  (32,31)      0.8573
  (33,32)      1.0753

that is result of a minimum spanning tree. now I want to extract 13,14,15,...26,28,29,...33. as seen 27 is not between numbers. so pred give: 13 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 that 20 and 33 is not. how can I extract total of numbers that say in top?

È stato utile?

Soluzione

[ii jj] = find(A);
answer = unique([ii(:); jj(:)]);

should do it.

Note that the find command with two outputs gives you the row and column index of all nonzero elements. Since you have a minimum spanning tree, each number you care about needs to occur at least once in the row or column (for example your matrix never has the number 29 in the first index, but it occurs in the second).

The unique function makes sure that each number that occurs is only represented once.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top