Error in setdiff function
Show older comments
Hi,
I have been tryin to use setdiff on two tables. There is a specific double column which contains NaN values and multiple rows which are same in both the tables. While using setdiff the rows that contains NaN in a specific column in both the tables comes as a difference between two tables, which should not happen. Both the rows are exactly same and the setdiff is considering NaN from same cells as different values. Is there a solution to this problem? Is there any other method to get the difference between the rows?
Accepted Answer
More Answers (1)
Are Mjaavatten
on 8 Jul 2021
Edited: Are Mjaavatten
on 8 Jul 2021
One workaround is to replace all NaNs with some spceific value that is not present in your data, say -9999:
>> S1 = [1,2,3,NaN,5,6];S2 =[2,3,5,NaN];
>> setdiff(S1,S2)
ans =
1 6 NaN
>> S1(isnan(S1)) = -9999;S2(isnan(S2)) = -9999;
>> setdiff(S1,S2)
ans =
1 6
>> S1(S1==-9999) =NaN;S2(S2==-9999) = NaN; % Restore originals
1 Comment
Are Mjaavatten
on 8 Jul 2021
Edited: Are Mjaavatten
on 8 Jul 2021
This function hopefully does what yout want:
function S = setdiffn(S1,S2)
dummy = rand;
while any(ismember(union(S1,S2),dummy))
dummy = rand; % Make sure dummy is not present in sets
end
S1(isnan(S1)) = dummy;S2(isnan(S2)) = dummy;
S = setdiff(S1,S2);
end
Categories
Find more on Logical 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!