Calculate mean, median by excluding any given number

前端 未结 1 984
别跟我提以往
别跟我提以往 2021-01-05 02:39

I have a matrix of size 5000*5000, with 90% values as 0. Is there a ready-made solution available to calculate the mean, median for this matrix after excluding \'0\' ?

相关标签:
1条回答
  • 2021-01-05 03:11

    If you want median and mean of overall matrix then try following

    median(x[x>0])
    mean(x[x>0])
    

    If you want median and mean row wise

    apply(x,1,function(x){mean(x[x>0])})
    apply(x,1,function(x){median(x[x>0])})
    

    If you want median and mean coloumn wise

    apply(x,2,function(x){mean(x[x>0])})
    apply(x,2,function(x){median(x[x>0])})
    
    0 讨论(0)
提交回复
热议问题