Getting a new matrix after comparing two matrices in some conditions

3 Ansichten (letzte 30 Tage)
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:nbL1
l=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
end
end
end
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?
  7 Kommentare
Clémentine OU
Clémentine OU am 4 Sep. 2017
@dpb: Sorry, I want to say 'K1'. I type the wrong things.
dpb
dpb am 4 Sep. 2017
Well, I'll leave the manipulations to build the 'K1' array to you; I still don't follow the explanation (what position is "2rd"? is only one confusing issue from not proofing your postings well enough in the last).
If your code does what you want as you imply, then the Answer I provided below will return the maximum of 2 rows of the matching value for each.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 4 Sep. 2017
Bearbeitet: dpb am 4 Sep. 2017
"... can check whether the rows of L1 are included in K1. I can't count the times and formulate new matrix R"
Well, if you understand the process of building the row to check for in the K1 array that we're unable to discern the logic behind, then
ia=ismember(L1,K1_fixedup(i));
is the logical array of which rows (if any) are in L1 of the i th fixed-up K1 row. Since logical true is 1 and false 0, the number is then
nK1_i = sum(ia);
To select those simply check for
L1_Want=L1(find(ia==1,2),:);
to return two (or fewer, find will return the number found up to the maximum requested. Consequently, you don't really even need to compute the number found explicitly.
I'd guess if the logic for building the expanded rows from the initial array were made more clear the whole thing could be reduced to an arrayfun or accumarray expression but the above should let you implement the accumulation of the desired output array from your existing code if it does what you expect except for the selection.
  1 Kommentar
Clémentine OU
Clémentine OU am 7 Sep. 2017
Bearbeitet: dpb am 7 Sep. 2017
@dpb:Thanks for your inspiration. I made the code finally for what I need.
R=[];
match=0;
for i=1:nbL1
l=L1(i,[1,2,5]);
S=[];
aa=0;
for j=1:nbK1
k=K1(j,[1,2,5]);
if K1(j,3)==0 & K1(j,4)==0 %(4 3 0 0 1)or(1 4 0 0 1)
disp('*-----*');
[match,loc]=ismember(l,k,'rows');
if match==1
R=[R;K1(j,:)];
end
elseif K1(j,3)~=0 %(1 4 2 0 1)
disp('#-----#');
[match,loc]=ismember(l,k,'rows');
if match==1
S=[S;K1(j,:)];
aa=aa+match
end
end
end
if aa>2
R=[R;S([1:2],:)];
elseif aa<=2
R=[R;S];
end
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by