Is there an alternative for the ismember() function in a for loop?
Show older comments
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
Answers (1)
David Hill
on 9 Dec 2020
Have you looked at the unique() function? It might work for you.
a=unique(A,'rows','stable');
3 Comments
Nigar Musayeva
on 9 Dec 2020
David Hill
on 9 Dec 2020
A simplified example would help me understand.
Nigar Musayeva
on 9 Dec 2020
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!