Wrong output of ismember command
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Dear all,
In part of my code I am using ismember command and as the array I am using has 21*9 shape and all of these elements are in the first element I expected an array of 21*9 members to be my out put while the output has shape of 22*9! Any idea about the cause? Please find my code in the following. I found the rows having same value in the 9th column in all_com variable and then used idx commands to find the rows corresponding to that.
Thank you in advance.
in1=load('in1.mat')
in2=load('in2.mat')
out1=load('out1.mat')
out2=load('out2.mat')
all_common= intersect(intersect(intersect(in1(:,9), in2(:,9)), out1(:,9)), out2(:,9));
idx1 = ismember(in1(:,9),all_common);
in1_com = in1(idx1,:);
idx2 = ismember(in2(:,9),all_common);
in2_com = in2(idx2,:);
idx3 = ismember(out1(:,9),all_common);
out1_com = out1(idx3,:);
idx4 = ismember(out2(:,9),all_common);
out2_com = out2(idx4,:);
0 Commenti
Risposta accettata
Walter Roberson
il 13 Lug 2022
filedata = load('in1.mat');
in1 = filedata.in1;
filedata = load('in2.mat');
in2 = filedata.in2;
filedata = load('out1.mat');
out1 = filedata.out1;
filedata = load('out2.mat');
out2 = filedata.out2;
all_common= intersect(intersect(intersect(in1(:,9), in2(:,9)), out1(:,9)), out2(:,9));
idx1 = ismember(in1(:,9),all_common);
in1_com = in1(idx1,:);
idx2 = ismember(in2(:,9),all_common);
in2_com = in2(idx2,:);
idx3 = ismember(out1(:,9),all_common);
out1_com = out1(idx3,:);
idx4 = ismember(out2(:,9),all_common);
out2_com = out2(idx4,:);
5 Commenti
Walter Roberson
il 13 Lug 2022
out2(69,9) and out2(235,9) are both 13740001 which is a member of all_common. In order to get the 21 results that you expect, you would need to decide between selecting row 69 or row 235; how do you want to decide which of the two to use?
Your logic of expecting the same number of rows as entries in all_common assumes that the entries in column 9 are unique, but they are not.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!