MATLAB: How to divide an image diagonally into two equal part where the image size is [168 300 3]

duplicate post requiring merging

divide the image diagonally into two part where the image size is [168 300 3]. how can it be done?

Best Answer

A simple way would be to use triu and tril. You could do it like this:
UpperTriangle = cat(3, triu(img(:, :, 1), triu(img(:, :, 2), triu(img(:, :, 3));
LowerTriangle = cat(3, tril(img(:, :, 1), triu(iml(:, :, 2), tril(img(:, :, 3));
Or like this:
mask = true(size(img, 1), size(img ,2));
UpperTriangle = img(repmat(triu(mask), [1 1 3]));
LowerTriangle = img(repmat(tril(mask), [1 1 3]));