Compare some parts from one row in two matrices

Hello, I have two matrices, one is
K=[1 4 2 0 1;1 4 0 0 1;2 4 0 0 1;2 4 3 0 1;5 4 2 0 2];
the other is
L=[1 4 0 0 1; 2 4 0 0 1; 4 2 0 0 2].
I want to do somethings:
  1. get one row from L in a loop;
  2. extract the first column and the last column of this row;
  3. compare the extracted number with the related location in every row of K;
  4. If the numbers are matched, get the index from K.Can anyone help me how to do that, please?Thanks in advance.

7 Comments

What have you tried so far?
@Clémentine OU: The instructions seems to be clear an easy. Please post, what you have tried so far and ask a specific question. The readers are more motivated to assist you, when you show any own effort.
Explain point 3: What is the "related location"? How do you want to store the results?
Related location meaning first and last column of K, I'd suppose
Sorry,'the related location' means the corresponding location.
Thank you for your suggestion. The code that I did as follows: [nbK,~]=size(K);
[nbL,~]=size(L);
J=[];
o1=[];
o2=[];
o5=[];
k1=[];
k2=[];
k5=[];
for i=1:nbL
o1=L(i,1);
o2=L(i,2);
o5=L(i,5);
for j=1:nbK
k1=K(j,1);
k2=K(j,2);
k5=K(j,5);
if o1==k1 & o2==k2 & o5==k5
J(i)=j;
end
end
end
From my code, I can find all the corresponding from every row of L. But I don't know how to store the indices from the loop.
Corresponding to what?
I have extracted the first column and the last column of every row in L. I want to compare whether they are matched with the first column and the last column of every row in K. Is it more clear?

Sign in to comment.

 Accepted Answer

K_sub = K(:,[1,end]);
L_sub = L(:,[1,end])
[ii,kk] = size(L_sub);
L_sub=reshape(L_sub',[1,kk,ii]);
result = squeeze(sum(bsxfun(@eq,K_sub,L_sub),2) == 2)
Where each column will be a logical index to where your condition is true.

2 Comments

@ José-Luis :Thanks for your reply. I've tried your method which can solve my problem well. You use 'reshape', 'bsxfun' and 'squeeze' commands which are enlarged my knowledge. Could you help me to solve bigger dimension problem, please? Now I want to locate that the elements(in 1st, 2nd and 5th columns) of every row in L is in which line of P.
K =
1 4 2 0 1
4 3 0 0 1
1 4 0 0 1
3 4 0 0 2
1 4 3 0 2
4 1 0 0 2
2 4 0 0 1
3 4 0 0 1
4 1 0 0 1
4 2 0 0 1
1 4 3 0 1
2 4 3 0 1
2 4 1 0 1
3 4 2 0 1
3 4 1 0 1
L =
1 4 0 0 1
2 4 0 0 1
3 4 0 0 1
4 1 0 0 1
4 2 0 0 1
4 3 0 0 1
1 4 0 0 2
3 4 0 0 2
4 1 0 0 2
4 3 0 0 2
I tried the similar code as follows:
K_sub = K(:,[1,2,end])
L_sub = L(:,[1,2,end])
[ii,kk,mm] = size(L_sub)
L_sub=reshape(L_sub',[1,kk,ii,mm])
result = squeeze(sum(bsxfun(@eq,K_sub,L_sub),3) == 3)
why my result shows nothing?
result =
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
You don't need mm. You still need to perform the sum along the second dimension.
K_sub = K(:,[1,2,end])
L_sub = L(:,[1,2,end])
[ii,kk] = size(L_sub)
L_sub=reshape(L_sub',[1,kk,ii])
result = squeeze(sum(bsxfun(@eq,K_sub,L_sub),2) == 3)
Please accept the answer that best solves your problem.
As an exercise, you could try to adapt it so you can specify the column indexes instead of hardcoding them.

Sign in to comment.

More Answers (1)

K = [1 4 2 0 1;1 4 0 0 1;2 4 0 0 1;2 4 3 0 1;5 4 2 0 2];
L = [1 4 0 0 1; 2 4 0 0 1; 4 2 0 0 2];
[match, locK] = ismember(L(:, [1,end]), K(:, [1,end]), 'rows')
Result = locK(match);

9 Comments

Probably OP has to use loops for this "homework" :P
Thanks for your reply. I tried to use this menthod, but I failed. Absolutely, you can check whether every row in L is in K as well as its corresponding location in K. But the result can't get all the corresponding locations. The results are shown as follows: L(:, [1,end])=1 1;2 1;4 2. K(:, [1,end])=1 1;1 1;2 1;2 1;5 2. match = 1 1 0 ;locK = 1 3 0 . For example, when pick some columns from the first row in L, which is 1 1. It is also in K, which is in 1st and 2nd rows, but the results only can store the 1st row. But I want all the corresponding index.
Jan
Jan on 30 Aug 2017
Edited: Jan on 30 Aug 2017
@Clémentine OU: I cannot follow the explanations. I still do not understand, which are the "related locations" and explaining it by the term "corresponding" does not clarify this also.
It would be useful if you create the wanted result manually for the given example input.
@KL: I bet, that this is no homework, because both inputs K and L contain 60% of completely unused data. Data for homework are usually "dense" to reduce the noise. If this is a homework (please, Clémentine, could you clarify this), I offer you a coffee or beer. :-)
@Jan Simon: My apologies. In fact, I want to extract every row of L to check whether it is in K. Normally, we can use 'ismember(L,K,'rows')'. But my check is a bit different. I want to check some parts(1st, 2nd and 5th column) of every row in L whether they are matched at 1st, 2nd and 5th column in every row of K. If they are matched, get the indices out from K. Is it clear? I think that it needs a loop to come it true. And you didn't use a loop, so your method can't get all indices out I think.
You forgot to answer Jan's questions:
  1. Is it homework?
  2. Can you post the expected output for your sample arrays?
I want to check some parts(1st, 2nd and 5th column) of every row
in L whether they are matched at 1st, 2nd and 5th column in every
row of K
I'm still not sure, what this means. What exactly are the "all indices" you want to get? ismember('rows') checks, if the row vectors or L(:, [1,5]) appear anywhere as row vectors in K(:, [1,5]). Please try to explain again, what you want instead. Try it in words and post the wanted output for the given example. Don't give and there is no need for apologies: This is your problem and you are the only one who suffers from not getting the wanted solution.
@Image Analyst and @Jan Simon: Thank you for your suggestion. Actually I just simply my problem. But I should post my original problem. I have two matrices K =
1 4 2 0 1
4 3 0 0 1
1 4 0 0 1
3 4 0 0 2
1 4 3 0 2
4 1 0 0 2
2 4 0 0 1
3 4 0 0 1
4 1 0 0 1
4 2 0 0 1
1 4 3 0 1
2 4 3 0 1
2 4 1 0 1
3 4 2 0 1
3 4 1 0 1
L =
1 4 0 0 1
2 4 0 0 1
3 4 0 0 1
4 1 0 0 1
4 2 0 0 1
4 3 0 0 1
1 4 0 0 2
3 4 0 0 2
4 1 0 0 2
4 3 0 0 2
I want to check that some elements in 1st, 2nd and 5th columns of every row in L are in which locations in K. Take an example of 1st row of L. I want to locate '1 4 1'(in 1st, 2nd and 5th columns of 1st row)in L is in which line of P. Obviously the answer is 1st,3rd,11th. As for elements in 2nd row of L,'2 4 1', the answers in K are 7th,12th and 13th rows.Just like it. I want to the exact locations of every row of L in P.
@KL: I am a newbie. I don't understand what you mean 'OP' and 'homework'. Some terminologies?
"OP" is the "original poster", the person who has asked the question.
"Homework" is a question given to pupils or students to be solved at home. If a user posts a homework question, the forum tend to explain, who it can be solved by your own. Posting a working solution could cause troubles, because you submitting this solution would be cheating and you had missed the chance to learn Matlab.
It is strange, that you do not want to post the wanted output for the shown input, although we have asked repeatedly for this. This is not useful. What's wrong with provided the information, which would allow for solving the problem?
Following your explanations here, this is a job for ismember('rows') and I still think that ismember('rows') solves the problem directly. At least you did not explain, what the differences between the result and your wanted results is.
@Jan Simon: Oh,thank you for your explanation. I'm a researcher. What I posted is just an issue in a part of my code. I tried to modify your code to show my meaning. Please see as follows:
Q1n=K(:, [1,2,end])
L1n=L(:, [1,2,end])
[nbL1n,~]=size(L1n);
[nbQ1n,~]=size(Q1n);
for i=1:nbL1n
l=L1n(i,:)
for j=1:nbQ1n
q=Q1n(j,:)
[match, locK] = ismember(l,q,'rows')
end
end
Then I could find the elements at 1st,2nd and 5th columns in every row of L whether they are included in P. But the problems coded in this way are that I can't get the exact indices from P. Because I want to extract all lines from P when the condition is true. Then I don't really understand why you add this syntax 'Result = locK(match)'. I can't see how it works. As for using 'ismember('rows')', you are right that it's a more direct way to solve my problem. But LocK can only get the lowest index in Q1n. I want to get all indices when match=1. I'm not sure that 'ismember('rows')' can make it true.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!