Extract Matrix Elements in Circular Manner

In summary: Displays the image with the circle mask.set(gcf, 'Position', get(0,'Screensize'));drawnow;In summary, to create a matrix that displays only those elements of 'A' which are inside a circle with its center at A(15,26), you would need to find all integer pairs ## (i,j) ## whose distance to the center ## (i_C, j_C)## is less than your specified distance ##R##.
  • #1
Atr cheema
69
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
  • #2
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.
 
  • #3
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);
 
  • #4
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);
 

What is meant by "Extract Matrix Elements in Circular Manner"?

"Extract Matrix Elements in Circular Manner" refers to a mathematical technique used to access specific elements within a matrix (a rectangular array of numbers or symbols). This technique involves starting at a designated point in the matrix and moving in a circular pattern to extract the desired elements.

Why is it important to extract matrix elements in a circular manner?

Extracting matrix elements in a circular manner allows for a more efficient and organized way of accessing specific elements within a matrix. It also ensures that all elements are accessed and reduces the chances of missing any important information.

How do you determine the starting point for extracting matrix elements in a circular manner?

The starting point for extracting matrix elements in a circular manner can be determined based on the desired element or pattern of elements you want to access. It can also be determined by the size and shape of the matrix itself.

Can the circular extraction method be applied to all types of matrices?

Yes, the circular extraction method can be applied to all types of matrices, including square matrices, rectangular matrices, and even irregularly shaped matrices.

Are there any limitations to using the circular extraction method?

One limitation of using the circular extraction method is that it may not be the most efficient way to access elements in very large matrices. In such cases, other techniques may be more suitable. Additionally, the circular extraction method may not work well for non-numeric matrices, such as text or image matrices.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Precalculus Mathematics Homework Help
Replies
1
Views
505
  • Precalculus Mathematics Homework Help
Replies
32
Views
815
  • Linear and Abstract Algebra
Replies
7
Views
1K
Back
Top