# Compare some parts from one row in two matrices

조회 수: 2(최근 30일)
Clémentine OU 2017년 8월 30일
편집: Clémentine OU 2017년 9월 1일
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표시 이전 댓글 수: 6숨기기 이전 댓글 수: 6
Clémentine OU 2017년 8월 30일
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?

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

### 채택된 답변

José-Luis 2017년 8월 30일
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표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
José-Luis 2017년 8월 31일
편집: José-Luis 2017년 8월 31일
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)
As an exercise, you could try to adapt it so you can specify the column indexes instead of hardcoding them.

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

### 추가 답변(1개)

Jan 2017년 8월 30일
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표시 이전 댓글 수: 8숨기기 이전 댓글 수: 8
Clémentine OU 2017년 9월 1일
편집: Clémentine OU 2017년 9월 1일
@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.

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

### 범주

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!