Wrong output of ismember command

2 views (last 30 days)
Hamid
Hamid on 13 Jul 2022
Commented: Hamid on 14 Jul 2022
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')
in1 = struct with fields:
in1: [265×9 double]
in2=load('in2.mat')
in2 = struct with fields:
in2: [251×9 double]
out1=load('out1.mat')
out1 = struct with fields:
out1: [271×9 double]
out2=load('out2.mat')
out2 = struct with fields:
out2: [256×9 double]
all_common= intersect(intersect(intersect(in1(:,9), in2(:,9)), out1(:,9)), out2(:,9));
Index in position 2 exceeds array bounds. Index must not exceed 1.
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,:);

Accepted Answer

Walter Roberson
Walter Roberson on 13 Jul 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 Comments
Walter Roberson
Walter Roberson on 13 Jul 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.
Hamid
Hamid on 14 Jul 2022
@Walter RobersonThanks a lot. I expected the elements in column 9 to be unique according to my results but it was no correct.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!