Is there an alternative for the ismember() function in a for loop?

조회 수: 1 (최근 30일)
Nigar Musayeva
Nigar Musayeva 2020년 12월 9일
댓글: Nigar Musayeva 2020년 12월 9일
I have a program that reads an xlsx file, finds unique rows in the first 3 columns and compares these unique rows to all the rows in the same 3 columns in the initial table that was imported. The goal is to find the indices of the matching rows to find duplicates. There is another column that contain values that I want to compare. So I want to find the duplicate rows and compare their values, and if they're differen, I store them in a new table. The problem is that the imported data is really large, so it takes a very long time for this function to finish what it's doing.
I would be thankful, if you had any suggestions that would help me speed up the process. Here you can see the code that I've described:
[file,path] = uigetfile('*.xlsx');
filename = [path file];
[~,~,raw_file] = xlsread(filename);
tabelle = cell2table(raw_file(2:end, 1:3));
unirow = unique(tabelle, 'rows', 'stable');
daten = cell2table(raw_file(2:end, :));
error_table = table;
idxrow = 1;
datum = datestr(datetime(raw_file(2,2)), 'yyyymmdd');
for ii = 1: size(unirow,1)
[lia,~] = ismember(tabelle,unirow(ii,:));
idx = find(lia);
% idx = find(cellfun(@(c)all(strcmp(unirow(ii,1:3),c)),num2cell(raw_file(2:end,1:3),2)));
werte = daten(idx, 9);
if size(unique(werte),1) > 1
for jj = 1:size(idx, 1)
error_table(idxrow, :) = daten(idx(jj,1),:);
idxrow = idxrow + 1;
end
end
end

답변 (1개)

David Hill
David Hill 2020년 12월 9일
Have you looked at the unique() function? It might work for you.
a=unique(A,'rows','stable');
  댓글 수: 3
David Hill
David Hill 2020년 12월 9일
A simplified example would help me understand.
Nigar Musayeva
Nigar Musayeva 2020년 12월 9일
let's say I have a data set like this one
Var1 Var2 Var3 Var4
______________________________ ____________ _____________________
'name1' '30.11.2020' '29.11.2020 16:44:00' [0,0,0]
'name1' '30.11.2020' '29.11.2020 16:44:00' [1,2,1]
'name3' '30.11.2020' '29.11.2020 16:45:00' [0,0,0]
'name4' '30.11.2020' '29.11.2020 16:44:00' [0,0,0]
'name1' '30.11.2020' '29.11.2020 16:44:00' [0,1,0]
'name3' '30.11.2020' '29.11.2020 16:45:00' [0,2,0]
so unirows stores theunique rows in columns 1 to 3. Then I use ismember to find the indices of the unique rows (for example row 'name1 30.11.2020 29.11.2020 16:44:00' has indices 1,2 and 5). I use these indices to compare the values in the 4th column.
I hope it's somehow clear:)

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by