I need to create a polygon or buffer along an irregular shaped coastline in a 2D array of gridded sea temperature data.
5 Comments
Hi Angus,
Your aim is to create a 578x235 grid of ocean temperature data with land pixels represented as NaN values. Additionally, you need to implement a 1-pixel buffer around the coastline, ensuring that all pixels adjacent to land pixels (NaNs) also become NaNs. In order to accomplish this task, I will start by generating a 578x235 grid of random ocean temperature data using the rand function, set the first 100 rows of the grid as land pixels by assigning NaN values to them. Then, create a A logical array nan_mask to identify NaN and non-NaN values in the ocean_data. Now, to implement the 1-pixel buffer around the coastline, I will iterate through each pixel in the grid (excluding borders). For each land pixel, I will check if any of its adjacent pixels are also land pixels using the nan_mask. If any adjacent pixel is land, I will set the current pixel as land as well in the buffered_data. Finally, I will display the ocean temperature data grid with the coastline buffer using imagesc and add a colorbar for reference. Here is the snippet code as an example to illustrate it.
% Create a 578x235 grid of ocean temperature data with land pixels as NaN
ocean_data = rand(578, 235); % Generate random ocean temperature data
ocean_data(1:100, :) = NaN; % Set the first 100 rows as land (NaN)
% Create a logical array to identify NaN and non-NaN values
nan_mask = isnan(ocean_data);
% Implement a 1-pixel buffer around the coastline
buffered_data = ocean_data;
for i = 2:size(ocean_data, 1)-1
for j = 2:size(ocean_data, 2)-1
if ocean_data(i, j) == 0 % Check if the pixel is land
if any(any(nan_mask(i-1:i+1, j-1:j+1))) % Check if any adjacent pixel is land
buffered_data(i, j) = NaN; % Set the pixel as land
end
end
end
end
% Display the ocean temperature data grid with the coastline buffer
imagesc(buffered_data);
colorbar;
title('Ocean Temperature Data with Coastline Buffer');
Please see attached plot.

I hope this answers your question.
Accepted Answer
More Answers (1)
5 Comments
Categories
Find more on Map Display 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!




