Wrong output of ismember command

조회 수: 2 (최근 30일)
Hamid
Hamid 2022년 7월 13일
댓글: Hamid 2022년 7월 14일
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,:);

채택된 답변

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

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by