Error in setdiff function

조회 수: 7 (최근 30일)
Sagar Gupta
Sagar Gupta 2021년 7월 7일
댓글: MEP 2022년 1월 25일
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?

채택된 답변

Bhavya Chopra
Bhavya Chopra 2021년 7월 8일
I understand that you want to find the difference between two rows. NaN values are not considered equal, and the logical inequality test, (NaN ~= NaN), also returns true. The documentation for function setdiff specifies that it treats NaN values as distinct.
You might find the isequaln function to be useful to determine array equality, which treats NaN values as equal to each other, and returns a logical value.
As another work-around to Are Mjaavatten's answer, to obtain the difference between rows, you can also use the following approach:
a = [3 4 5 NaN NaN]; % Considering two example vectors
b = [3 NaN];
a_temp = a(~isnan(a)); % Removing NaN values using isnan() function
b_temp = b(~isnan(b));
setdiff(a_temp, b_temp) % Using setdiff to obtain difference
  댓글 수: 1
MEP
MEP 2022년 1월 25일
Hi, I have the same problem. My goal is to compare two tables and I want to use setdiff only that the NaN should be treated as the same and not as different. It is absurd that there isn't a dedicated option on the function to do this.

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

추가 답변 (1개)

Are Mjaavatten
Are Mjaavatten 2021년 7월 8일
편집: Are Mjaavatten 2021년 7월 8일
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
Are Mjaavatten
Are Mjaavatten 2021년 7월 8일
편집: Are Mjaavatten 2021년 7월 8일
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

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

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by