I have a large matrix that contains various features extracted from microscopic cell images. The different features are distributed across the columns, the individual cells across the rows of that matrix. However, the measurements come from time lapse microscopy, such that each individual cell has 90 rows (time points) in that matrix. So this matrix has the dimension [cell_amount*90; feature_amount].
My goal is to:
- calculate the difference of subsequent time points for each cell (the "derivative" of the time series), and then
- create a new matrix that contains an aggregation of those differences for each cell (so that new matrix has the dimension [cell_amount; feature_amount]).
I set up some code in R to test my problem, where I have 4 cells, 4 features (columns) and each cell has 3 time point values. So the first cell would be on rows 1-3, the second on row 4-6, and so on. From this I calculate the difference of the values:
A <- matrix(sample(1:100, 4*12), ncol = 4)
B <- abs( A - dplyr::lag(A) )
B[seq(1,nrow(B), 3),] <- NA
This results in a matrix where the first row of each cell contains NA values:
[,1] [,2] [,3] [,4]
[1,] NA NA NA NA
[2,] 82 29 54 22
[3,] 32 44 18 31
[4,] NA NA NA NA
[5,] 22 61 10 33
[6,] 19 64 54 35
[7,] NA NA NA NA
[8,] 59 18 6 10
[9,] 34 47 70 6
[10,] NA NA NA NA
[11,] 60 23 68 22
[12,] 17 13 12 9
The resulting matrix containing an aggregation for those values for each cell, in this case the variance, should then look like:
[,1] [,2] [,3] [,4]
[1,] 1250 112.5 648 40.5
[2,] 4.5 4.5 968 2
[3,] 312.5 420.5 2048 8
[4,] 924.5 50 1568 84.5
How can I calculate this new matrix in R? Any help is appreciated.