0

我们有以下矩阵result

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 

我想执行以下场景:从第一列开始,我们希望将每个非唯一值限制为仅在矩阵中出现一次。因此,以 col1 作为起点,矩阵的其余部分应重新排列为:

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      

现在我们看到 col4 有最独特的元素,所以我们考虑它的值继续第二次重新排列,结果是:

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 

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

先感谢您。

4

1 回答 1

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

希望有帮助!

于 2012-12-02T17:04:25.743 回答