Pergunta

We have the following matrix 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

Its column unique element index size is (without zeros):

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 

I want to perform the following scenario: Starting from the first column we want to restrict each non-unique value to be present only once in our matrix. So with col1 as starting point the rest of matrix should be rearranged as:

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      

Now as we see col4 has the most unique elements, so we consider its values to continue to second re-arrangement and the result is:

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    

Doing that as many times as necessary, in that example twice more for col5 and col8 we reach the desired result:

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 

Which is the most efficient way to perform this? May I see your suggestions please ?

Thank you in advance.

Foi útil?

Solução

Your question is poorly worded, so the following is a step-by-step breakdown of what I managed to understand from it.

Lets suppose you have the following matrix:

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) To count the number of unique elements in each column, just invoke unique on each column and count the non-zero elements:

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

2) To nullify all recurring elements of column #1, we can just do this:

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

Now we need to iterate over all columns and nullify all non-unique elements, so we do that with a loop. The final solution is therefore:

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

which yields your desired result:

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

Hope that helps!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top