1

Let say I have 3 MATs

X = [ 1 3 9 10 ];
Y = [ 1 9 11 20];
Z = [ 1 3 9 11 ];

Now I would like to find the values that appear only once, and to what array they belong to

1
  • The values that appear once in one of the vectors, or appear once counting the three vectors? In your example no value appears twice in a single vector, but they do if you count the three vectors. Commented Oct 10, 2016 at 7:39

2 Answers 2

1

I generalized EBH's answer to cover flexible number of arrays, arrays with different sizes and multidimensional arrays. This method also can only deal with integer-valued arrays:

function [uniq, id] = uniQ(varargin)
combo = [];
idx = [];
for ii = 1:nargin
    combo = [combo; varargin{ii}(:)]; % merge the arrays
    idx = [idx; ii*ones(numel(varargin{ii}), 1)];
end
counts = histcounts(combo, min(combo):max(combo)+1);
ids = find(counts == 1); % finding index of unique elements in combo
uniq = min(combo) - 1 + ids(:); % constructing array of unique elements in 'counts'
id = zeros(size(uniq));
for ii = 1:numel(uniq)
    ids = find(combo == uniq(ii), 1); % finding index of unique elements in 'combo'
    id(ii) = idx(ids); % assigning the corresponding index
end

And this is how it works:

[uniq, id] = uniQ([9, 4], 15, randi(12,3,3), magic(3))

uniq =

     1
     7
    11
    12
    15


id =

     4
     4
     3
     3
     2
Sign up to request clarification or add additional context in comments.

1 Comment

Nice! I didn't have time to continue generelizing it.
1

If you are only dealing with integers and your vectors are equally sized (all with the same number of elements), you can use histcounts for a quick search for unique elements:

X = [1 -3 9 10];
Y = [1 9 11 20];
Z = [1 3 9 11];
XYZ = [X(:) Y(:) Z(:)]; % one matrix with all vectors as columns
counts = histcounts(XYZ,min(XYZ(:)):max(XYZ(:))+1);
R = min(XYZ(:)):max(XYZ(:)); % range of the data
unkelem = R(counts==1);

and then locate them using a loop with find:

pos = zeros(size(unkelem));
counter = 1;
for k = unkelem
    [~,pos(counter)] = find(XYZ==k);
    counter = counter+1;
end
result = [unkelem;pos]

and you get:

result =

    -3     3    10    20
     1     3     1     2

so -3 3 10 20 are unique, and they appear at the 1 3 1 2 vectors, respectively.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.