Uniqe ID in list of indexed 2D/3D Image masks (or 2D/3D matrix) in R : <p>I want to make a "global" ID of 2D/3D nuclear masks across a list of images.
When i segment my cells they are numbered from 1:numberOfCellsInEachImage. but I would like to have an uniqe Id for each cells across all images.</p>

<p>I have already tried to make a forloop with a global counter and assigned IDs acording to the counter, but It became a messy large thing that I do not trust compleatly</p>

<pre><code># minimal exampe
img1 = matrix(data = 0, nrow = 15, ncol = 15)
img1[2:3, 2:3] = 1
img1[9:10, 5:6] = 2
img1[2:4, 11:13] = 3
img1
# Example indexed image 1
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
# [1,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# [2,]    0    1    1    0    0    0    0    0    0     0     3     3     3     0     0
# [3,]    0    1    1    0    0    0    0    0    0     0     3     3     3     0     0
# [4,]    0    0    0    0    0    0    0    0    0     0     3     3     3     0     0
# [5,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# [6,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# [7,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# [8,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# [9,]    0    0    0    0    2    2    0    0    0     0     0     0     0     0     0
#[10,]    0    0    0    0    2    2    0    0    0     0     0     0     0     0     0
#[11,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
#[12,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
#[13,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
#[14,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
#[15,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
img2 = matrix(data = 0, nrow = 15, ncol = 15)
img2[3:4, 2:4] = 1
img2[10:11, 5:7] = 2
img2[3:5, 11:14] = 3
img2
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
# [1,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# [2,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# [3,]    0    1    1    1    0    0    0    0    0     0     3     3     3     3     0
# [4,]    0    1    1    1    0    0    0    0    0     0     3     3     3     3     0
# [5,]    0    0    0    0    0    0    0    0    0     0     3     3     3     3     0
# [6,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# [7,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# [8,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# [9,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
#[10,]    0    0    0    0    2    2    2    0    0     0     0     0     0     0     0
#[11,]    0    0    0    0    2    2    2    0    0     0     0     0     0     0     0
#[12,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
#[13,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
#[14,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
#[15,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0

# This is the data structure I have
# 
listOfImages2D = list(img1, img2)
#x = listOfImages2D[[1]]
listOfImages3D = lapply(listOfImages2D, function(x){
  x_out = array(data = 0, dim = c(15, 15, 3) )
  x_out[,,2] = x
  return(x_out)
})
</code></pre>

<pre><code>#What I want
# I want all numbers to become an uniqe ID across images
# All zeros should remain zero
# I use img1[img1 == 0 ] = 0 #after giving uniqe IDs
# Image one should ideally start at 1

# ## desired output 
# listOfImages2D_global_index[[1]]
# # same as above
# 
# listOfImages2D_global_index[[2]]
# # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
# # [1,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# # [2,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# # [3,]    0    4    4    4    0    0    0    0    0     0     6     6     6     6     0
# # [4,]    0    4    4    4    0    0    0    0    0     0     6     6     6     6     0
# # [5,]    0    0    0    0    0    0    0    0    0     0     6     6     6     6     0
# # [6,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# # [7,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# # [8,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# # [9,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# #[10,]    0    0    0    0    5    5    5    0    0     0     0     0     0     0     0
# #[11,]    0    0    0    0    5    5    5    0    0     0     0     0     0     0     0
# #[12,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# #[13,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# #[14,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# #[15,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0
# 
# # the code should also work for 3 dimensions
</code></pre>
