Reputation: 80
I do not know if this is the exact site for my question, but as far as I understand the FAQ correctly one can ask questions regarding one specific software algorithm from one specific language.
I have several 2D matrices (containing NaN's) for which I want to draw one iso-/contour line in Matlab. For this I use the implemented contour() function in the following way, where Matlab automatically chooses the level of the contour lines and I only specify the number of levels I want to display:
Contourline=contour(MyData,1);
The results are looking fine, but I was wondering now how the exact height of my contour line is chosen and if the heights are comparable between different matrices, meaning are they always drawn at e.g. 50 % of the maximum value?
The contour() function also allows to manually set a height for the contour line. Doing this (by setting the heights to x % of the maximum value in my matrix), the results appear not as fitting as the ones from the automated approach (regardless of the value of 'x' I choose).
Thanks!
Edit:
My question aims at the underlying algorithm implemented in the contour() function when the number of levels is set to 1. This produces very good results for my data, meaning the function draws one line (or more if the heights are the same) which kinda catches the distribution in my data (Image). If I try to set the heights manually contour(myData,[X X])
this works,but not for all of my matrices in contrast to the automated mode when I only specify the number of levels contour(myData,1)
. So I am quite happy with those results, but if anybody asked me how I analyzed my data I want to give them a more mathematically sound answer than just: "I chose this function in this program".
In general:
I want to know how this height is chosen for the automated mode and if there might be a part in the code which makes it undesirable to compare the contour lines for different matrices. The documentation only states that the heights are set automatically, but not exactly how.
Upvotes: 1
Views: 436
Reputation: 65460
You can use a vector to specify at what height to draw the contours
% Draws contours at height = 1 and 2
contour(data, [1 2])
If you want only one height, just provide that value twice
% Contour at height 1
contour(data, [1 1])
If you want to just specify the number of levels to draw (and not the heights), you can use the LevelList
property of the graphics object to determine at what height the lines were drawn.
[c, h] = contour(data, 1);
levels = get(h, 'LevelList')
You can also use the LevelList
property to modify the height of the levels after creation.
[c, h] = contour(data, 1);
% A level at 2
set(h, 'LevelList', 2);
% Or two levels
set(h, 'LevelList', [1 2])
Update
As far as how MATLAB selects the levels when you specify the number of levels, it creates equally spaced levels between the minimum and maximum of the input data using the following formula:
minimum = min(real(double(data(:)));
maximum = max(real(double(data(:)));
tmp = linspace(minimum, maximum, nLevels + 2);
levels = tmp(2:end-1);
Upvotes: 1