Finding 2D area defined by contour lines in Matlab

squishy picture squishy · Nov 18, 2013 · Viewed 7k times · Source

I am having difficulty with calculating 2D area of contours produced from a Kernel Density Estimation (KDE) in Matlab. I have three variables:

X and Y = meshgrid which variable 'density' is computed over (256x256) density = density computed from the KDE (256x256)

I run the code

contour(X,Y,density,10)

This produces the plot that is attached. For each of the 10 contour levels I would like to calculate the area. I have done this in some other platforms such as R but am having trouble figuring out the correct method / syntax in Matlab.

C = contourc(density)

I believe the above line would store all of the values of the contours allowing me to calculate the areas but I do not fully understand how these values are stored nor how to get them properly.

Any help or suggestions would be greatly appreciated! Thank you very much.

Answer

Leeor picture Leeor · Nov 18, 2013

Here is a similar question I posted regarding the usage of Matlab contour(...) function.

The main ideas is to properly manipulate the return variable. In your example

c = contour(X,Y,density,10)

the variable c can be returned and used for any calculation over the isolines, including area.