Especificar valores de una matriz dentro de un poligono

조회 수: 3 (최근 30일)
José Juan Ibáñez Cemborain
José Juan Ibáñez Cemborain 2023년 7월 25일
답변: Keshav 2023년 8월 1일
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!