4

This link shows that there is a kronecker delta function in matlab. However:

>> help kroneckerDelta

    kroneckerDelta not found

I am using R2011b, so maybe this wasn't programmed into the toolkit yet?

EDIT:: It works in MuPad, just not in matlab...

.

yankeefan11
  • 485
  • 1
  • 6
  • 18
  • Also for future reference, you would have to check your version documentation as you may note that link provides R2013a documentation (so do all searches). So try from the Mathworks >> Support >> Documentation Center >> Other Release. – voxeloctree Jul 18 '13 at 20:01

4 Answers4

4

The Kronecker delta returns 1 if j==k...

So you could simplify the expression with:

function d=kronDel(j,k)

d=j==k

end

Luckily, MATLAB represents booleans as (0,1)

Christopher Creutzig
  • 8,656
  • 35
  • 45
  • You should format your code. Also, this question is tagged [matlab], not [octave], so `end` should be used. It would probably also be a good idea to cast the output to `double` or the class of the input variables. – horchler Jan 29 '15 at 16:15
2

I don't see it in my R2012b so perhaps not. Unless you need the symbolic math, you could always write your own. Something as simple as

function d = kronDel(j,k)

if j == k
    d = 1;
else
    d = 0;
end
voxeloctree
  • 839
  • 6
  • 13
2

You can also do this inline, like

( a == b )

However, using an anonymous function is a nice way to convert a one liner like this into something that is more readable

kronDel = @(j, k) j==k ;

kronDel( 2, 1 )
kronDel( 2, 2 )
Peeter Joot
  • 7,848
  • 7
  • 48
  • 82
0

Your link to is to the MuPAD function kroneckerDelta -note the URL and the funky typography of the examples. You won't see it in any version of Matlab because it's only available through MuPAD (type mupad in your command window and try in the window that launches). I have no idea when it was added to MuPAD, I know that it's at least in R2012b. You may have it even if the help command returns nothing.

If you have kroneckerDelta in R2011b, you won't be able to run it from the regular command window or Editor in the normal fashion.

evalin(symengine,'kroneckerDelta(1,1)')

or the more flexible

feval(symengine,'kroneckerDelta',1,1)

See more here. However, if you're not working with symbolic math, there's really no reason to use this function that I can see -it's not even vectorized! I'd go with a solution that fully emulates kroneckerDelta's behavior in double precision:

function d=kronDel(m,n)
if nargin == 1
    d = double(m==0);
else
    d = double(m==n);
end
horchler
  • 18,384
  • 4
  • 37
  • 73