在矩阵中找到比例列 [英] Finding proportional columns in matrix

查看:158
本文介绍了在矩阵中找到比例列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大矩阵(1,000行和50,000列).我知道有些列是相关的(排名仅为100),我怀疑有些列甚至是成比例的.如何找到这样的比例列? (一种方法是循环corr(M(:,j),M(:,k))),但是还有效率更高的吗?

I have a big matrix (1,000 rows and 50,000 columns). I know some columns are correlated (the rank is only 100) and I suspect some columns are even proportional. How can I find such proportional columns? (one way would be looping corr(M(:,j),M(:,k))), but is there anything more efficient?

推荐答案

如果通过除以每一列的最大值来对其进行归一化,则比例将变为相等.这使问题变得更容易.

If you normalize each column by dividing by its maximum, proportionality becomes equality. This makes the problem easier.

现在,要测试是否相等,可以在列上使用一个(外部)循环;使用bsxfun可以轻松地对内部循环进行矢量化处理.为了提高速度,请仅将每列与其右侧的列进行比较.

Now, to test for equality you can use a single (outer) loop over columns; the inner loop is easily vectorized with bsxfun. For greater speed, compare each column only with the columns to its right.

为节省一些时间,结果矩阵也已预先分配为近似大小(您应该设置该大小).如果近似大小错误,那么唯一的代价就是速度会稍慢一些,但是代码可以正常工作.

Also to save some time, the result matrix is preallocated to an approximate size (you should set that). If the approximate size is wrong, the only penalty will be a little slower speed, but the code works.

通常,浮点值之间的相等性测试应包括公差.

As usual, tests for equality between floating-point values should include a tolerance.

结果以2列矩阵(S)形式给出,其中每行包含成比例的两行索引.

The result is given as a 2-column matrix (S), where each row contains the indices of two rows that are proportional.

A = [1 5 2 6 3 1
     2 5 4 7 6 1
     3 5 6 8 9 1]; %// example data matrix
tol = 1e-6; %// relative tolerance

A = bsxfun(@rdivide, A, max(A,[],1)); %// normalize A
C = size(A,2);
S = NaN(round(C^1.5),2); %// preallocate result to *approximate* size
used = 0; %// number of rows of S already used
for c = 1:C
    ind = c+find(all(abs(bsxfun(@rdivide, A(:,c), A(:,c+1:end))-1)<tol));
    u = numel(ind); %// number of columns proportional to column c
    S(used+1:used+u,1) = c; %// fill in result
    S(used+1:used+u,2) = ind; %// fill in result
    used = used + u; %// update number of results
end
S = S(1:used,:); %// remove unused rows of S

在此示例中,结果为

S =
     1     3
     1     5
     2     6
     3     5

表示第1列与第3列成正比;第1列与第5列成比例,依此类推.

meaning column 1 is proportional to column 3; column 1 is proportional to column 5 etc.

这篇关于在矩阵中找到比例列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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