Transforming two vectors (2D plot points) and color data (1D vector) into 2D matrix color data (for contour plot)

3 views (last 30 days)
Hi, I would like to transform two data vectors (A,B) and a vector of color data (C, between 0 and 1) into a 2D matrix with entry values corresponding to color (or height). Any tips would be appreciated. I tried using meshgrid but am not sure how to distribute my color data to each point within the newly formed matrix.

Accepted Answer

Aurele Turnes
Aurele Turnes on 8 Aug 2014
In order to distribute the color information in C to a 2D-grid of points indexed by (A,B), you would need the number of points in C to be equal to the number of points in the grid. So if A is a length N vector and B is a length M vector, C will need to have N*M entries. You will have to format C as a matrix of size N-by-M where entry C(i,j) corresponds to the height (or color) at the coordinate (A(i),B(j)) in the (x,y)-plane. Then, you can use the contour function as follows:
contour(A,B,C)
However, if all your vectors A, B and C are length N, you do not have enough information to fill in the color value for all the points in the grid formed by abscise A and ordinate B. You need additional information to fill in the color data for the missing point. For instance: do all the points with the same abscise A(i) correspond to the same color?
If you do not have the information for the missing points in the grid, you can try filling all the missing points with one value that is not already in C to create an artificial color level. Then the known points that correspond to coordinates (A(i),B(i)) with color C(i) will be on the diagonal of your new N-by-N matrix D. For instance, you can try:
% fill a matrix with value min(C)-1
D = (min(C(:))-1)*ones(N);
% put the known color points on the diagonal
D(sub2ind([N N], 1:N, 1:N)) = C;
% plot the contour levels
contour(A,B,D);
You can also specify which levels you want to draw (so that the artificial level created by adding the unknown points do not appear). For instance, picking one out of two values in C:
V = C(1:2:N);
contour(A,B,D,V);
Refer to the documentation page for contour for more information.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!