How to compare elements of rows within cell array?

3 views (last 30 days)
Hey All ... I have an array 'array1'. I want to compare each elements of array1 with other elements.
array1= {[1,2,3,8];[1,6];[1,2,3];[1,5,6];[2,4,5];[1,7];[2,8];[2,3,5,7,9]}
e.g. first elements is array1{1,1}={[1,2,3,8]}
All these values 1,2,3,8 will be compared with all other elements of array1 one by one. And find out where that value is present in other elements of array1, index of that row will be saved in resultant array.
ResultantArray{1,1} = {[2,3,4,6];[3,5,7,8];[3,8];[7]} % e.g. [2,3,4,6] is result of comparing '1' with others and 2,3,4,6 are rows where 1 is present in array1.
ResultantArray{2,1} = {[1,3,4,6];[4]} % similary [1,6] are compared with others and {[1,3,4,6];[4]} are rows where these are present.
ResultantArray{3,1} = {[1,2,4,6];[1,5,7,8];[1,8]}
and so on for other elements of array1.
please help.

Accepted Answer

Birdman
Birdman on 10 Jan 2018
for k=1:size(array1,1)
for j=1:numel(array1{k,1})
for i=1:size(array1,1)
[~,idx(i,j,k)]=ismember(array1{k,1}(j),array1{i,1});
end
ResultantArray{k,j}=setdiff(find(idx(:,j,k)),k);
end
end

More Answers (1)

KSSV
KSSV on 10 Jan 2018
A for loop and ismember does the job......read about ismember
  1 Comment
lucksBi
lucksBi on 10 Jan 2018
Yes i try this before posting question but i was doing some mistake with 3rd loop which @Birdman corrected accurately. Thank You

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!