Question: For the following equation:

![enter image description here](https://i.stack.imgur.com/BubSp.png)

Do we write it in `MATLAB` as follows:

```
sum=0;
    for j=1:k
        for i=1:n
          sum = sum + (u(i,j)*log2(u(i,j)+(1-u(i,j)*log2(1-u(i,j)))))/n;  
        end
    end
    result = -1 * sum;

```

Thanks.

Answer: 
**to quote Jongware:** *no, that's not how we do it!*

you rather write:

```
fun = @(x) x*log2(x)+(1-x)*log2(1-x);
result = -1/n*sum( arrayfun(fun,u(:)) )

```
```
sum=0;
for i=1:k
    for j=1:n
      sum = sum + (u(i,j)*log2(u(i,j)+(1-u(i,j)*log2(1-u(i,j)))))/n;  
    end
end
result = -1 * sum;

```