Find the rows that have common elements for 4 arrays

조회 수: 7 (최근 30일)
Hamid
Hamid 2022년 7월 12일
댓글: Hamid 2022년 7월 13일
Dear all, I have 4 files and created 4 arrays, I wanted to make a new array that includes the rows that their last column elements have same values. I used ismember commadn for 2 arrays but not sucessful for 4 files. Please find the files attached and my code as the following:
in1 = readtable ('in1_dmp.txt');
in2 = readtable ('in2_dmp.txt');
out1 = readtable ('out1_dmp.txt');
out2 = readtable ('out2_dmp.txt');
maxCas=5000;
in1 = table2array (in1);
casNum_m = in1(:,9);
btchNum_m = in1(:,10);
for i = 1 : length(btchNum_m)
eventNUm=(casNum_m+maxCas.*(btchNum_m-1));
end
%Delete column
in1(:,9:11) = [];
%Add new column
in1 = [in1 eventNUm];
in2 = table2array (in2);
casNum_m = in2(:,9);
btchNum_m = in2(:,10);
for i = 1 : length(btchNum_m)
eventNUm=(casNum_m+maxCas.*(btchNum_m-1));
end
%Delete column
in2(:,9:11) = [];
%Add new column
in2 = [in2 eventNUm];
out1 = table2array(out1);
casNum_n = out1(:,9);
btchNum_n = out1(:,10);
for i = 1 : length(btchNum_n)
eventNUm=(casNum_n+maxCas.*(btchNum_n-1));
end
%Delete column
out1(:,9:11) = [];
%Add new column
out1 = [out1 eventNUm];
out2 = table2array(out2);
casNum_n = out2(:,9);
btchNum_n = out2(:,10);
for i = 1 : length(btchNum_n)
eventNUm=(casNum_n+maxCas.*(btchNum_n-1));
end
%Delete column
out2(:,9:11) = [];
%Add new column
out2 = [out2 eventNUm];
idx1 = ismember(in1(:,9),in2(:,9));
in1_com = in1(idx1,:);
%
% idx2 = ismember(in2(:,9),in1(:,9));
% in2_com = in2(idx2,:);
%
% idx3 = ismember(out1(:,9),out2(:,9));
% out1_com = out1(idx3,:);
%
% idx4 = ismember(out2(:,9),out1(:,9));
% out2_com= out2(idx4,:);
  댓글 수: 2
Jan
Jan 2022년 7월 12일
What is the purpose of this loop:
for i = 1 : length(btchNum_m)
eventNUm=(casNum_m+maxCas.*(btchNum_m-1));
end
The body does not depend on i, so running it repeatedly is a waste of time only. This occurs several times, which is even more confusing.
Hamid
Hamid 2022년 7월 12일
@Jan Thank you for the comment. The aim was to create a new column by multiplying two columns. At the end I have 4 variables which I want to know which rows has the same value in their last columns as attached.

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

채택된 답변

Jan
Jan 2022년 7월 12일
What about:
intesect(intersect(intersect(in1(:,9), in2(:,9)), out1(:,9)), out2(:,9))
  댓글 수: 3
Jan
Jan 2022년 7월 13일
Which rows? Do you mean the 4 vectors of indices of the common values according to the 4 variables?
Hamid
Hamid 2022년 7월 13일
the rows that have the elements of these common elements.
common=intesect(intersect(intersect(in1(:,9), in2(:,9)), out1(:,9)), out2(:,9))
I used ismember to find them but faced with the following problem. Could you please take a look? Many thanks.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by