Convert xy Level Coordinates to Matrix, which data is negative and non-integer numbers
2 views (last 30 days)
Show older comments
My goal is to get the edge of the light spot according to the density of the contour line. After using the getContourLineCoordinates function to the contour matrix which is return by the contour function, I get the X,Y and Level values (Contour height value) of the contour, I want to convert these values to matrix just according the X,Y,Level values(no interpolation required), and let the matrix display in imagesc has the same effect as contour.
Answers (1)
Shubham Dhanda
on 13 Jun 2023
Hi,
I understand that you want to create a matrix based on X, Y, & Level values and display the matrix using ‘imagesc’ with same colour as the contour. From the information provided by you, I am assuming the following values of X, Y and Level as an example.
X = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
Y = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
Level = [0.4, -0.1, -0.3, -0.2, 0.1, 0.3, 0.7, 0.9, 0.6, 0.2]
Below is the MATLAB code to create a matrix and display the matrix using
‘imagesc’:
X = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
Y = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0]
Level = [0.4, -0.1, -0.3, -0.2, 0.1, 0.3, 0.7, 0.9, 0.6, 0.2]
% Determine the size of the matrix
x_min = floor(min(X));
x_max = ceil(max(X));
y_min = floor(min(Y));
y_max = ceil(max(Y));
matrix_size = [y_max - y_min + 1, x_max - x_min + 1];
% Create the matrix and initialize it with NaN values
matrix = NaN(matrix_size);
% Convert X,Y values to linear indices
x_int = round((X - x_min) + 1); % Convert X values to integers
y_int = round((Y - y_min) + 1); % Convert Y values to integers
coords = [x_int(:), y_int(:)]; % Combine the X,Y values into a matrix
linear_indices = sub2ind(matrix_size, coords(:,2), coords(:,1)); % Compute the linear indices
% Fill in the values at the corresponding indices in the matrix
matrix(linear_indices) = Level(:);
% Display the matrix using imagesc
imagesc(matrix);
colorbar;
See Also
Categories
Find more on Gamma Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!