-3

The hamacher sum is :
enter image description here

I wrote the following MATLAB code for the above function

function f=hamachersum(x,y)
f = zeros(numel(x),1);
for j=1:numel(x)
    if x(j)==1 && y(j)==1
        f(j,1)=1;
    else 
        f(j,1)=(x(j)+y(j)-2*(x(j)*y(j)))/(1-(x(j)*y(j)));
    end
end
end

Then I want to test t3=hamachersum(t1,t2)
My input values t1, t2 are

t1

t1 =

1.0000
     0
1.0000
1.0000
1.0000
1.0000
   NaN
0.8167
1.0000
1.0000
1.0000
0.4667
   NaN
1.0000
1.0000
1.0000
   NaN
   NaN
1.0000
1.0000
1.0000
   NaN
0.0250
1.0000

t2

t2 =

1.0000
0.5524
1.0000
1.0000
1.0000
1.0000
   NaN
     0
1.0000
1.0000
1.0000
1.0000
   NaN
1.0000
1.0000
1.0000
   NaN
   NaN
0.6032
1.0000
1.0000
   NaN
0.9973
0.7260

The result is

t3 =

    1.2000
    0.5524
    1.0000
    1.0000
    1.0000
    1.0000
       NaN
    0.8167
    2.0000
    2.0000
    1.0000
    1.0000
       NaN
    0.6667
    1.0000
    1.0769
       NaN
       NaN
    1.0000
    1.0000
    1.0000
       NaN
    0.9973
    1.0000

Why do I get values above 1. As this is a fuzzy operator it can't have values above 1.

Is there something wrong in my code?

pnuts
  • 58,317
  • 11
  • 87
  • 139
sam_rox
  • 739
  • 3
  • 14
  • 29
  • 2
    Setting `t1=[0:0.01:1]';` `t2=t1`, `t3=hamachersum(t1,t2)` results in values between 0 and 1, as does it when you use `t2=flipud(t1)`. I can't seem to find a fault in this code, running it on R2012a – Adriaan Sep 14 '15 at 15:09
  • 3
    You are not displaying a truthful `t3`. Plugging in the first elements `t1(1) == 1` and `t2(1) == 1` clearly yields `1`, not `1.2`. Please display the real answer. – Mad Physicist Sep 14 '15 at 16:21
  • @MadPhysicist This is the output I get when run on R2014a – sam_rox Sep 14 '15 at 16:35
  • @Adriaan I ran it on R2014a. I don't know what is wrong but this is the output I get – sam_rox Sep 14 '15 at 16:50
  • Please provide a minimal example to reproduce your problem. I am unable to reproduce it! – Daniel Sep 14 '15 at 16:55
  • @Daniel If I use `k1=[1,0,1,1,1]; >> k2=[1,0.5524,1,1,1]; >> k3=hamachersum(k1,k2) k3 = 1.0000 0.5524 1.0000 1.0000 1.0000` Here it works correctly. But for some reason for the above `t1 `and `t2` it produces values above 1, which I can't understand – sam_rox Sep 14 '15 at 17:03
  • 3
    @sam_rox: As the inputs "1" and "1" produce different outputs, I assume the values are not really "1" but rather close to "1". With the image it is impossible to reproduce the problem. – Daniel Sep 14 '15 at 17:10
  • @Daniel They are "1" . I included the MATLAB values for `t1` and `t2` – sam_rox Sep 14 '15 at 17:24
  • @sam_rox I just ran your code with those values, my output is different. Are you sure you are not overwritting t3 somewhere? I get: `t3 = 1.0000 0.5524 1.0000 1.0000 1.0000 1.0000 NaN 0.8167 1.0000 1.0000 1.0000 1.0000 NaN 1.0000 1.0000 1.0000 NaN NaN 1.0000 1.0000 1.0000 NaN 0.9973 1.0000` – Ander Biguri Sep 14 '15 at 17:37
  • I would suggest changing `if x(j)==1 && y(j)==1` to `if x(j)>=1 && y(j)>=1` – Ander Biguri Sep 14 '15 at 17:39
  • @AnderBiguri I entered values of t1 and t2 to new variables as `a1,a2` and used `a3=hamachersum(a1,a2)`. Then also I get the output as `t3` . I changed as `if x(j)>=1 && y(j)>=1` . Still I get `1.2` as the first value. – sam_rox Sep 14 '15 at 17:47
  • 1
    @sam_rox sorry, but It just makes no sense. Probably you have some other variables called t1 or t2, or you have some t1.m or t3.m or something like that. If you close Matlab, create a new directory, and there paste your function and execute it with the values you given us, you get the output I said, not the one you said, thus the Question here makes no sense and should be closed. – Ander Biguri Sep 14 '15 at 17:51

1 Answers1

3

I am probably answering this against my better judgement.

Caveat: I am not familiar with the Hamacher sum so my approach to answering this is strictly based on the equation in your question.


Is there something wrong in my code?

Your code produces 1.0 when I run it on MATLAB R2014a for inputs of 1.0 and 1.0, I'm presuming that is correct as you have an explicit condition for it. I cant produce the results you are are seeing in your question.

However, I felt compelled to provide a more efficient implementation of the equation

function h = hamachersum(mu_a, mu_b)
    h = (mu_a + mu_b - (2 .* mu_a .* mu_b)) ./ (1 - mu_a .* mu_b);
    % h(isnan(h)) = 1.0; % Included this line to show you how to remove NaN
end

Note: I've included % h(isnan(h)) = 1.0; to show you how to handle cases when mu_a and mu_b are both 1.0 as you have explicitly handled this in your question (rather poorly might I add).


Comparing floating point numbers is not reliable even in MATLAB and could be part of the reason why you are receiving the results you are. A better way to check what the value of a floating point number is would be to use

if (x - 1.0 > 1e-15)
    fprintf(1, "x == 1.0");
else
    fprintf(1, "x ~= 1.0");
end

If x is equal to 1.0 down to machine precision this expression will be true otherwise it will be false.

IKavanagh
  • 6,089
  • 11
  • 42
  • 47