comparing elements of a cell array based on another cell array

조회 수: 1 (최근 30일)
lucksBi
lucksBi 2018년 3월 6일
댓글: lucksBi 2018년 3월 6일
hi
array1 = {[2,5];[1,3,4]}
array2= {[1,4],[1,5],5,4,[3,5,2]}
I need to perform a comparison. 1st element of array1 is [2,5] which is at position 1 in array1. so it will take 1st value in array2 [1,4] and compare it with 2nd and 5th value in array2 which are [1,5] and [3,5,2]. and in result it will take out the one which has at least one value in common with [1,4] as here we will get 2 because 2nd element [1,5] has '1' common with [1,4].
for 2nd element in array1 which is [1,3,4], it will take 2nd value of array2 and compare it with 1st,3rd and 4th value and [1,3] will be in result because 4th value which is [4] has nothing in common with [1,5].
so output will be:
ResultantantArray = {2,[1,3]}
i hope i have explained it well. kindly help on this.
Thanks in anticipation
  댓글 수: 1
Jos (10584)
Jos (10584) 2018년 3월 6일
What did you try yourself in pseudocode, using loops, for instance? Also see
help intersect

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2018년 3월 6일
편집: Andrei Bobrov 2018년 3월 6일
a1 = {[2,5];[1,3,4]};
a2= {[1,4],[1,5],5,4,[3,5,2]};
n = numel(a1);
out = cell(n,1);
nn = cellfun(@numel,a2);
for ii = 1:n
jj = a1{ii};
t = repelem(jj,nn(jj));
out{ii} = unique(t(ismember([a2{jj}],a2{ii})));
end
  댓글 수: 3
lucksBi
lucksBi 2018년 3월 6일
yup i added 'unique'... Thanks alot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by