Hello,
I have an issue which has bothered me for a while. Please give me some suggestions. Thanks in advance. I have two matrices:
K1 = 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 L1 = 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
What I want to do is that how many times of every row in L1 are included in matrix K1 and the corresponding indices of K1. Then if the times of every row in L1 included in K1 are over 2 times, I will keep only 2 rows of K1 and formulate a new matrix R. I just want to get this matrix R.
I will explain it in details. For example, the first line of L1 is [1 4 0 0 1] which is included in rows of K1,[1 4 2 0 1;1 4 0 0 1;1 4 3 0 1]. Then I want to get the indices:[1;3;11]. Obviously, the times of 1st row in L1 is over 2, So I will just keep the first two indices(1 and 3) and delete the 3rd(11). Then put them in a new matrix R. Now, R=[1 4 2 0 1;1 4 0 0 1]. Then the 2nd line of L1 follows this process. I will take the 5th line of L1 as another example, which is [4 2 0 0 1]. It is included in rows of K1,[1 4 2 0 1; 4 2 0 0 1;3 4 2 0 1]. The number of indices is also over three. Then delete the last one. Then R=[1 4 2 0 1;1 4 0 0 1;…;1 4 2 0 1; 3 4 2 0 1].
What I've done now is as follows
R=[]; for i=1:nbL1l=L1(i,[1,2,5]) for j=1:nbK1 k1=K1(j,[1,2,5]) k2=K1(j,[2,3,5]) if K1(j,3)==0 & K1(j,4)==0 %(4 3 0 0 1)or(1 4 0 0 1)
disp('0-0') [match,index]=ismember(l,k1,'rows') if index~=0 R=[R;K1(index,:)] end elseif K1(j,3)~=0 & K1(j,4)==0 %(1 4 2 0 1)
if L1(i,1)==4 %(4 3 0 0 1)
disp('0-1') [match,index]=ismember(l,k2,'rows') if index~=0 R=[R;K1(index,:)] end else disp('0-2') [match,index]=ismember(l,k1,'rows') if index~=0 R=[R;K1(index,:)] end end endendend
I only can check whether the rows of L1 are included in K1. I can't count the times and formulate new matrix R that I want. Could anyone help me, please?
Best Answer