How to compare elements of rows within cell array?

조회 수: 3 (최근 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개)

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