How to compare elements of rows within cell array?

13 ビュー (過去 30 日間)
lucksBi
lucksBi 2018 年 1 月 10 日
コメント済み: lucksBi 2018 年 1 月 10 日
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 件のコメント
lucksBi
lucksBi 2018 年 1 月 10 日
Accurate. Thank You

サインインしてコメントする。

その他の回答 (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

サインインしてコメントする。

カテゴリ

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!

Translated by