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!