2012-12-02 53 views
0

我们有以下的矩阵resultMatlab的:分组和最小化矩阵的唯一列元素的索引

result = 
    Columns 1 through 13 
    3  1  1  1  1  1  6  2  3  6  2  1  6 
    4  3  3  5  7  5 10 10  4 10  6  9  8 
    6  4  4  7  9  7  0  0  0  0  0  0  0 
    10  5  5  8  0  0  0  0  0  0  0  0  0 
    Columns 14 through 25 
    2 10  3 10  3  8  8  0  0  0  0  0 
    8  0  0  0  0  0  0  0  0  0  0  0 
    0  0  0  0  0  0  0  0  0  0  0  0 
    0  0  0  0  0  0  0  0  0  0  0  0 

其列独特元素的索引规模(不为零):

Indexes of result: 
    Columns 1 through 13 
    4  4  4  4  3  3  2  2  2  2  2  2  2 
    Columns 14 through 25 
    2  1  1  1  1  1  1 

我想执行以下场景: 从第一列开始,我们想要限制每个非唯一值在矩阵中仅出现一次。因为我们看到COL4拥有最独特的元素

result = 
    Columns 1 through 13 
    3  1  1  1  1  1  0  2  0  0  2  1  0 
    4  0  0  5  7  5  0  0  0  0  0  9  8 
    6  0  0  7  9  7  0  0  0  0  0  0  0 
    10  5  5  8  0  0  0  0  0  0  0  0  0 
    Columns 14 through 25 
    2  0  0  0  0  8  8  0  0  0  0  0 
    8  0  0  0  0  0  0  0  0  0  0  0 
    0  0  0  0  0  0  0  0  0  0  0  0 
    0  0  0  0  0  0  0  0  0  0  0  0 
Indexes of result (without zeros): 
    Columns 1 through 13 
    4  2  2  4  3  3  0  1  0  0  1  2  1 
    Columns 14 through 25 
    2  0  0  0  0  1  1  

现在,让我们考虑它的价值,继续第二重排,结果是: 因此,与COL1为出发点矩阵的其余部分应被重新排列:

result = 
    Columns 1 through 13 
    3  0  0  1  0  0  0  2  0  0  2  0  0 
    4  0  0  5  0  0  0  0  0  0  0  9  0 
    6  0  0  7  9  0  0  0  0  0  0  0  0 
    10  0  0  8  0  0  0  0  0  0  0  0  0 
    Columns 14 through 25 
    2  0  0  0  0  0  0  0  0  0  0  0 
    0  0  0  0  0  0  0  0  0  0  0  0 
    0  0  0  0  0  0  0  0  0  0  0  0 
    0  0  0  0  0  0  0  0  0  0  0  0 

Indexes of result (without zeros): 
    Columns 1 through 13 
    4  0  0  4  1  0  0  1  0  0  1  1  0 
    Columns 14 through 25 
    1  0  0  0  0  1  1  

否则,按照需要多次,在例如两次为COL5和col8我们达到预期的结果:

result = 
    Columns 1 through 13 
    3  0  0  1  0  0  0  2  0  0  0  0  0 
    4  0  0  5  0  0  0  0  0  0  0  0  0 
    6  0  0  7  9  0  0  0  0  0  0  0  0 
    10  0  0  8  0  0  0  0  0  0  0  0  0 
    Columns 14 through 25 
    0  0  0  0  0  0  0  0  0  0  0  0 
    0  0  0  0  0  0  0  0  0  0  0  0 
    0  0  0  0  0  0  0  0  0  0  0  0 
    0  0  0  0  0  0  0  0  0  0  0  0 

Indexes of result (without zeros): 
    Columns 1 through 13 
    4  0  0  4  1  0  0  1  0  0  0  0  0 
    Columns 14 through 25 
    0  0  0  0  0  0  0 

哪是执行此操作的最有效方法? 我可以看看你的建议吗?

预先感谢您。

+0

你的问题是什么? –

+0

@EitanT我的问题是找到一种方法来执行这个特定的分组 - 最小化加上找到最有效的方式来做到这一点。 – professor

+0

你不是自己实现它吗?那么你是如何得到这些结果的? –

回答

1

你的问题措辞不佳,所以下面是什么,我设法从它来了解一个一步一步的细分。

允许假设有以下矩阵:

result=[3 1 1 1 1 1 6 2 3 6 2 1 6 2 10 3 10 3 8 8 0 0 0 0 0; 
     4 3 3 5 7 5 10 10 4 10 6 9 8 8 0 0 0 0 0 0 0 0 0 0 0; 
     6 4 4 7 9 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 
     10 5 5 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 

1)要计算在每一列中独特元件的数目,只是调用unique每一列上并计数非零元素:

count = arrayfun(@(n)sum(unique(result(:, n)) ~= 0), 1:size(result, 2)) 

2)以抵消的#1列所有重复的元素,我们可以做这样的:

idx = arrayfun(@(n)ismember(result(:, n), result(:, 1)), 2:N, 'Uniform', 0); 
result(logical([idx{:}])) = 0 

现在我们需要遍历所有列并取消所有非唯一元素,所以我们用循环来完成。因此,最终的解决方案是:

N = size(result, 2); 
ii = 0; 
while (ii <= N) 

    % # Count the number of unique elements in each column 
    count = arrayfun(@(n)sum(unique(result(:, n)) ~= 0), 1:N); 

    % # Advance to the next column with the maximum number of unique elements 
    ii = ii + find(count(:, ii + 1:N) == max(count(:, ii + 1:N)) & count(ii + 1:N), 1); 
    if isempty(ii) 
     break 
    end 

    % # Nullify non-unique elements starting from column i 
    idx = arrayfun(@(n)(ismember(result(:, n), result(:, ii)) & n ~= ii), 1:N, 'Uniform', 0); 
    result(logical([idx{:}])) = 0; 
end 

这将产生你想要的结果:

result= 
    3 0 0 1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    4 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    6 0 0 7 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    10 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

希望帮助!

+1

亲爱的先生,非常感谢。我真的很感激你的时间和精力来帮助一个未知的人。你的解决方案是现货。做得好@EitanT。附:我没有在您的个人资料中看到您的电子邮件,请通过我的电子邮件与我联系。 – professor

+1

很高兴帮助。这是StackOverflow的用途! –