Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201977

Finding interconnected neighbours in a 2d matrix

$
0
0

I have a similar matrix as below

  a| b | c | d | e 
1|1| 0 | 0 | 1 | 0
2|0| 0 | 1 | 0 | 1
3|0| 1 | 0 | 0 | 0
4|0| 1 | 0 | 1 | 0
5|0| 1 | 0 | 0 | 0
6|1| 0 | 0 | 0 | 0

i want to get the neighbors for each alphabets. neighbours are defined as follows for eaxample "a" and "d" are 1 for row 1 hence they are neighbours now "d" is 1 for row 1 and 4 hence any alphabet which is 1 for row 4 will be neighbour for "d" as well as "a". Here "b" is 1 for row 4 and hence will be neigbour to bot "a" and "d". And so on..

the output should be

   a    | b     |  c    |  d    |  e 
   3    | 3     |  2    |  3    |  2
 abd    |abd    |     ce|    abd|     ce
 1,3,4,6|1,3,4,6|    2,5|1,3,4,6|    2,5

where second rows tells the number of neighbours(including the alphabet itself). third row tells the neigbours fourth row tells the rowname connecting these neighbours


Viewing all articles
Browse latest Browse all 201977

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>