Extract Matrix Elements in Circular Manner

Click For Summary

Discussion Overview

The discussion revolves around extracting elements from a matrix in a circular manner, specifically focusing on how to select elements within a defined radius from a central point in the matrix. Participants explore various methods and code implementations related to this problem, which involves both theoretical and practical aspects of matrix manipulation.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Experimental/applied

Main Points Raised

  • One participant proposes a method to extract elements from a matrix based on their distance from a specified center point, suggesting the use of the inequality for circular selection.
  • Another participant expresses a concern about losing index information when creating a new matrix that contains only the selected elements, emphasizing the need to maintain the original matrix's structure.
  • A later reply provides a code example that demonstrates how to create a circular mask to isolate elements within a specified radius, while also addressing the visualization of the results.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best method to extract elements while preserving index information. Multiple approaches are discussed, and some participants express uncertainty about the effectiveness of their proposed solutions.

Contextual Notes

Some limitations include the potential complexity of the methods for larger matrices and the need for clarity on how to maintain index positions in the resulting matrix. The discussion also highlights the importance of visual verification of the results.

Atr cheema
Messages
67
Reaction score
0
Lets say I have a matrix A=rand(31,51). How can I extract its elements from its center (say row = 15, column = 26) in circular manner. I want to have a matrix that displays only those elements of 'A' which are inside a circle with its center at A(15,26). Radius of circle can be any number say 5 cells from A(31,51).
 
Physics news on Phys.org
A naive solution would be to find all integer pairs ## (i,j) ## whose distance to the center ## (i_C, j_C)## is less than your specified distance ##R##.

In other words find solutions ##(i,j)\in \mathbb{N}## of the inequality ##(i-i_C)^2 + (j-j_C)^2 \leq R^2##.

I'm sure there are other, better solutions (especially for large distances in large matrices). I can't think of any of the top of my head.

When getting this to work you can check your work by plotting a disc of radius R around your center.
Overlay this with a grid of points representing each element of the matrix (i.e. use their indices as coordinates).
Finally you plot the solutions in another color and see whether all relevant elements are marked.
 
This code does somehow similar thing but I lose information about indices in the final array 'B'. I want matrix 'B' in such a way that its size is equal to size of A but it contains only selected values at those specific positions (indices) where they were in A. In other words, I don't want to lose information on indices as well.
Code:
A=rand(31,51);
xc=15; yc=26; r=5;
[x,y]=meshgrid(1:size(A,1),1:size(A,2));
mask=(x-xc)^2+(y-yc)^2<r^2;
B=A(mask);
 
Try this:

Code:
% Demo to mask an array with a circle.
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures.
clear;  % Erase all existing variables.
workspace;  % Make sure the workspace panel is showing.
fontSize = 20;

A=rand(31,51);
% Get the dimensions of the image.  numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(A);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(A, []);
% Change imshow to image() if you don't have the Image Processing Toolbox.
title('Original Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Image Analysis Demo','numbertitle','off')

% Initialize parameters for the circle,
% such as it's location and radius.
circleCenterX = 26; % Column
circleCenterY =  15; % row
circleRadius = 5;    % big circle radius

% Initialize an image to a logical image of the circle.
circleImage = false(rows, columns);
[x, y] = meshgrid(1:columns, 1:rows);
circleImage((x - circleCenterX).^2 + (y - circleCenterY).^2 <= circleRadius.^2) = true;
% Display it in the upper right plot.
subplot(2,2,2);
imshow(circleImage, []);
% Change imshow to image() if you don't have the Image Processing Toolbox.
title('Circle Mask', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
drawnow;

% Mask the image with the circle.
maskedImage = A; % Initialize with the entire image.
maskedImage(~circleImage) = 0; % Zero image outside the circle mask.

% Display it in the lower right plot.
subplot(2, 3, 5);
imshow(maskedImage, []);
% Change imshow to image() if you don't have the Image Processing Toolbox.
title('Image masked with the circle.', 'FontSize', fontSize);
 

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K