通过对应于列的块的最大值对块之间进行归一化 [英] Normalize between-blocks by the maxima of the block corresponding to the columns

查看:82
本文介绍了通过对应于列的块的最大值对块之间进行归一化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是我的上一个问题的后续内容 我不想通过局部最大值对块进行归一化,而是希望通过与列相对应的块的最大值对块之间进行归一化.

This question is a follow-up on my previous question Instead of normalizing blocks by the local maxima, I would like to normalize between-blocks by the maxima of the block corresponding to the columns.

#dummy data
mat <- matrix(round(runif(90, 0, 50),),9,9)
rownames(mat) <- rep(LETTERS[1:3],3)
colnames(mat) <- rep(LETTERS[1:3],3)

#Normalizes within and between blocks by the maxima of the focal block
ans <- mat / ave(mat, rownames(mat)[row(mat)], colnames(mat)[col(mat)], FUN = max)

#Number of blocks
sum(ans == 1)
#[1] 9

我想通过与列对应的块的最大值来规范块之间,即ABACBABCCACB.例如,在AB的情况下,通过BB中的max()将其标准化,并在CC中通过max()对其进行AC标准化.

I would like to normalize between-blocks, i.e.,AB, AC, BA, BC, CA, CB by the maxima of the block corresponding to the columns. E.g., in the case of AB normalize it by the max() in BB, and AC by the max() in CC etc.

> mat[rownames(mat)=="A",colnames(mat)=="B"]
  B  B  B
A 26 26 14
A 12 11 18
A 44 44 29

> mat[rownames(mat)=="B",colnames(mat)=="B"]
  B  B  B
B  9 23 20
B 28 45 28
B 14 12 45

在这种情况下,不是通过该块的最大值(即44),而是通过块BB的最大值(即45)来标准化块之间的AB.

In this case, normalizing the between-block AB not by the maxima of this block (i.e. 44), but by the maxima of block BB (i.e. 45).

任何指针都受到高度赞赏!

Any pointers are highly appreciated!

推荐答案

cn为向量,方法是先将mat的每个元素替换为其列名,然后逐列展开结果矩阵.类似地,对给出rn的行执行相同的操作.

Let cn be the vector formed by first replacing each element of mat with its column name and then unravelling the resulting matrix column by column. Similarly do the same thing with the rows giving rn.

(cn == rn) * matmat相同,除了所有非对角线块均被清零.

(cn == rn) * mat is the same as mat except all non-diagonal blocks are zeroed out.

v是一个向量,其名称是唯一的列名称,其值是相应对角线块的最大值. v的构造取决于最大值为0或更大的事实.

v is a vector whose names are the unique column names and whose values are the maxima of the corresponding diagonal blocks. The construction of v depnds on the fact that the maxima are 0 or more.

replace(mat, TRUE, v[cn])是通过将mat的每个元素替换为其列中对角线块的最大值而形成的矩阵,最后我们将mat除以该矩阵.

replace(mat, TRUE, v[cn]) is the matrix formed by replacing each element of mat with the maximum of the diagonal block in its column and finally we divide mat by that.

请注意,如果对角线块全为零,则该列将全部为NaN;否则,该列将全部为NaN.但是,如果任何非对角线块都为零,则应该没有任何问题.

Note that if any diagonal block is all zeros then the column will be all NaNs; however, there should not be any problem if any off-diagonal blocks are all zeros.

cn <- colnames(mat)[col(mat)]
rn <- rownames(mat)[row(mat)]
v <- tapply((cn == rn) * mat,  cn, max)
mat / replace(mat, TRUE, v[cn])

这篇关于通过对应于列的块的最大值对块之间进行归一化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆