Especificar valores de una matriz dentro de un poligono

5 次查看(过去 30 天)
Buenas tardes.
Teniendo una matriz de 256x256 enteramente con valores 0.5.
Me gustaría ponerle un valor concreto (por ejemplo 0.8) a los valores de la matriz que estén dentro de una forma geometrica delimitada por unas coordenadas x e y.
Lo he intentado con regionfill pero no soy capaz.
Muchas gracias y quedo a vuestra disposción.

采纳的回答

Keshav
Keshav 2023-8-1
To set specific values within a geometric shape in a matrix, you can use 'poly2mask' function. The 'poly2mask' function sets pixels that are inside the polygon to 1 and sets pixels outside the polygon to 0. Afterwards, you can utilize the logical indexing to assign the specific value in the matrix.
Below is a sample code for reference:
% Create a 256x256 matrix filled with 0.5 values
matrix = ones(256,256) * 0.5;
% Change the x and y coordinate according to your requirement.
x = [1, 10, 1, 10];
y = [5, 5, 15, 15];
% Create a logical mask for the shape
mask = poly2mask(x, y, 256, 256);
% Set the desired value (e.g., 0.8) within the shape
matrix(mask) = 0.8;
You can read more about 'poly2max' function in the below MathWorks documentation.

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!