How to compare elements of rows within cell array?

4 次查看(过去 30 天)
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.

采纳的回答

Birdman
Birdman 2018-1-10
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

更多回答(1 个)

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by