0

I have a matrix of 256*256 ,I need to break this matrix to get 1024 blocks (256*256/(8*8)). Then I need calculate mean,standard deviation,Kurtosis,standard deviation of each block (1024 blocks)

The dataset which I want from from matrix is

S.no Mean standard deviation Kurtosis Skewness 1 2 . . . . 1024

I am stuck in looping and retrieving the values to compute the statistics.

Praveen Chougale
  • 373
  • 3
  • 11
  • 1
    Sounds like a challenge. Where are you stuck exactly? Can you show us your code? Also please read - [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Shique May 29 '18 at 07:53
  • 1
    Possible duplicate: [Function to split a matrix into sub-matrices in R](https://stackoverflow.com/questions/24299171/function-to-split-a-matrix-into-sub-matrices-in-r/24299527#24299527) – markus May 29 '18 at 07:59
  • Yes sure... I have converted an Image to a matrix of 256*256.First I am trying to calculate mean,I am getting error,Below is the code. y <- resize(image, w = 256, h = 256) View(y) result=data.frame((y)) colnames(result) <- c("i","Mean") for (i in 1:nrow(result)) { for (j in 1:ncol(result)) { squared <- mean(result[i:8, j:8]) result[i, 2] <- squared } } – Praveen Chougale May 29 '18 at 08:00
  • matsplitter<-function(M, r, c) { rg <- (row(M)-1)%/%r+1 cg <- (col(M)-1)%/%c+1 rci <- (rg-1)*max(cg) + cg N <- prod(dim(M))/r/c cv <- unlist(lapply(1:N, function(x) M[rci==x])) dim(cv)<-c(r,c,N) cv } asa <-matsplitter(y,8,8) This code works to split the matrix in 1024 smaller block ,but I am not able to calculte the mean,standard deviation,skewness,kurtosis,as I am not able to read the output. – Praveen Chougale May 29 '18 at 08:08
  • @PraveenChougale Try `apply(X = asa, MARGIN = 3, FUN = mean)`. It should return a vector of length 1024. – markus May 29 '18 at 08:22
  • Yes thanks a lot got it.But I need summary of each block,by using FUN=mean ,I am getting only mean – Praveen Chougale May 29 '18 at 08:36
  • 1
    @PraveenChougale Please put the additional information from your comments in your question, i.e. **edit your question:** https://stackoverflow.com/posts/50579071/edit – jogo May 29 '18 at 10:04

1 Answers1

1

m is your matrix. yourMatrixFunction is a function you need to define that works on a single 8x8-matrix and acquires what you want.

m <- matrix(1,nrow=256,ncol=256)

first <- seq(1,256,by=8)
last  <- rep(8,length(first)) %>% cumsum

pair  <- Map(function(...)cbind(...),first,last) %>% expand.grid(.,.)
first <- pair[[1]]
last  <- pair[[2]]

all_m <- Map(function(x,y) m[x[1]:x[2],y[1]:y[2]],x=first,y=last)

lapply(all_m,yourMatrixFunction)
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69