How do I define the are of checkboard

3 views (last 30 days)
Leonard Valko
Leonard Valko on 28 May 2020
Answered: Parag on 31 Jan 2025
Hello, I have a problem for with area of integration in Monte Carlo method.
The script for the integrated 2D function I used and worked is
% square
res = (abs(x)<= a ) && (abs(y)<= a );
% circle
res = ( (x^2 + y^2) <= a^2 );
where res being the map or area where I want to integrate, a = 1 for these purposes and x and y are the variables of f(x,y).
Now the map I want looks like the one on the picture. I've tried cycles and writing all the conditions for x and y but nothing works. Can you please help ?

Answers (1)

Parag
Parag on 31 Jan 2025
It looks like you are trying to define an integration area in MATLAB using a Monte Carlo method. The image shows a checkerboard-like pattern within a square region. To define such a region in MATLAB, you need to apply a condition that alternates between included and excluded areas.
Here’s how you can define the checkerboard pattern in MATLAB:
% Define grid resolution
n = 100;
x = linspace(-1,1,n);
y = linspace(-1,1,n);
[X, Y] = meshgrid(x, y);
% Define checkerboard pattern (3x3 grid)
a = 2/3; % Size of each square in the 3x3 grid
res = mod(floor((X + 1) / a) + floor((Y + 1) / a), 2) == 1;
% Plot the region
figure;
imagesc(x, y, res);
colormap(gray);
axis equal;

Categories

Find more on Image Processing Toolbox 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!