Find the rows that have common elements for 4 arrays

7 Ansichten (letzte 30 Tage)
Hamid
Hamid am 12 Jul. 2022
Kommentiert: Hamid am 13 Jul. 2022
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 Kommentare
Jan
Jan am 12 Jul. 2022
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 am 12 Jul. 2022
@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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 12 Jul. 2022
What about:
intesect(intersect(intersect(in1(:,9), in2(:,9)), out1(:,9)), out2(:,9))
  3 Kommentare
Jan
Jan am 13 Jul. 2022
Which rows? Do you mean the 4 vectors of indices of the common values according to the 4 variables?
Hamid
Hamid am 13 Jul. 2022
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by