필터 지우기
필터 지우기

how to get common elements of matrix based on another matrix?

조회 수: 2 (최근 30일)
lucksBi
lucksBi 2018년 1월 5일
댓글: lucksBi 2018년 1월 5일
hey all
how to get common elements of array1 based on 'rows' array?
array1 = {[1,5,7,2];[1,4,6,2];[1,4,5]}
rows = {[2,3];[1,3];[1,2]}
result{1,1} = {[];[1,2];[1,5]} % compare row1 with row1 in array1 then row1 with row2 then row1 with row3
result{2,1} = {[1,2];[];[1,4]} % compare row2 with row1 in array1 then row2 with row2 then row2 with row3
result{3,1} = {[1,5];[1,4];[]} % compare row3 with row1 in array1 then row3 with row2 then row3 with row3
if we consider rows{1,1} then comparision will be between row 1 and other rows in aaray1 and so on.
  댓글 수: 11
lucksBi
lucksBi 2018년 1월 5일
Yes i have tried it but it compares elements of array1 with 'row' Like first comparison gives 2 As 2 is present in first row of array1 and same for others.
lucksBi
lucksBi 2018년 1월 5일
@Birdman @Guillaume Thanks to both of you for giving time to my query. I am accepting the more close answer to what i wanted. Thank You

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

채택된 답변

Guillaume
Guillaume 2018년 1월 5일
Still don't know what the actually result should be since you haven't explained why row 1 is compared to itself when it's not in rows. Ignoring this, and just comparing row i to the rows listed in rows{i} this will work:
result = cellfun(@(ar, r) arrayfun(@(rr) intersect(ar, array1{rr}), r, 'UniformOutput', false), array1, rows, 'UniformOutput', false)
result is a cell array the same size as rows and array1. Each cell of result is itself a cell array the same size as rows{i}.
  댓글 수: 1
lucksBi
lucksBi 2018년 1월 5일
Yes its closer to what i want. Thank You so much for your time.

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

추가 답변 (1개)

Birdman
Birdman 2018년 1월 5일
One approach(with a for loop):
for j=1:size(rows,1)-1
for i=1:size(array1,1)
result{i,j}=array1{i}(ismember(array1{i},rows{i}(j)));
end
end
Note: the ones with no intersect are eliminated, therefore the result is 3x2 cell.
  댓글 수: 7
Birdman
Birdman 2018년 1월 5일
Well, your question is hard to understand and my answer is almost the same what you wanted, so whether taking and modifying it or not is totally up to you.
lucksBi
lucksBi 2018년 1월 5일
Yes i am trying to modify it. Thanks alot

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by