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

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 Comments

I do not understand the explanation and do not understand how result is generated. Can you give clearer explanations?
Actually i am comparing rows of array 1. elements of 'rows' array gives elements which will be compared. Like first element is rows{1,1}={[2,3]}, As it is rows{1,1} means first element so it will compare first row of array1 with elements given in rows{1,1} which are 2,3 so first row of array1 will be compared with row2 and row3 of array1 and common elements will be taken in result. In result{1,1}, first element is [] becuase here comparision is row1 to row1 so thats why i left it blank.
Be careful with your notation,
row{1} = {[2, 3]}
is not the same as
row{1} = [2, 3]
%or
row(1) = {[2, 3]}
You state that row{1} = [2, 3] and because this is row{1} we compare the first row of array1. We compare it to the rows specified by row{1} so that's 2 and 3. So why, do you then state that "first element is [] because here comparision is row 1 to row 1" when 1 never appears in row{1}?
What if row{1} was [3, 3]? What would result{1} be?
Thankyou for explaining I ll take care of it.
"first element is [] because here comparison is row 1 to row 1" means that if we are comparing a row to itself then it does not matter what is the output.We can put a zero or [] or even the same value at this place.
And row{1} will always be [2,3] due to some logic at previous steps.
Yes, but why are you comparing row 1 to row 1, when row{1} only tell you to compare row 1 to row 2 and 3? row{1} is not [1, 2, 3]. That's the bit I don't understand with your logic.
Yes you are right. I didn't think of that. Sorry about confusion.
So what should the result (or the comparison) be?
In addition when comparing array{1} = [1 5 7 2] to array{2} = [1 4 6 2], why is the result [2] when the common elements are [1 2]?
Result will just contain common elements based on above mentioned condition.
This is a mistake. I have edited it.
My code actually does what you want. Have you tried it?
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.
@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

Sign in to comment.

 Accepted Answer

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 Comment

Yes its closer to what i want. Thank You so much for your time.

Sign in to comment.

More Answers (1)

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 Comments

I have no idea if this is the answer the OP is looking for (it does not produces the result asked for) but
x(ismember(x, y))
is the same as
intersect(x, y)
as long as there are no repeated elements in x.
Hey Thanks for your time. but it gives following output which i am unable to understand. Can you please explain?
result= {2,[];1,[];1,[]}
Also i need to keep values with no intersect also. But these may be replaced by zero instead of empty brackets.
My answer should not produce an outcome like that, it results in a 3x2 cell.
I hope my explanation to question clarifies what i am actually trying to get
Ok so it is comparing values in rows to array1. Like 2 is present in first element so it gives 2. But i need to compare rows within the array1. 'rows' array just gives the row no which will be comapred
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.
Yes i am trying to modify it. Thanks alot

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 5 Jan 2018

Commented:

on 5 Jan 2018

Community Treasure Hunt

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

Start Hunting!