Matlab Question(About matrix sampling)

In summary, the conversation is about a person asking for help with a MATLAB project that involves reading in an image and sampling random regions of it. They have figured out how to generate a random index and are now looking for a way to generate a matrix-shaped region of the image with a designated center. Another person suggests using the random index values to subsample the matrix and provides a code snippet as an example.
  • #1
miyakooo
1
0
Hi, y'all!

I'm trying to figure out how to do this MATLAB project for school.
We're required to read in an image and then sample random regions(square, matrix-shaped) of it.

I read in the image and it is formed by a lot of numbers(pixels?), and I determined how to generate a random integer that will index to a random point of the image. I know I am supposed to generate a matrix with the random point in its center, but I am clueless as to how to generate a e matrix-shaped region of an image with a designated center...any ideas? Help me please!

Thanks:)
Miyakooo
 
Physics news on Phys.org
  • #2
If you have your random index values, say i and j, why don't you use them to subsample your matrix? p = 1/2 subsample region in pixels

submatrix = image((i-p):(i+p), (j-p):(j+p))
This should give a submatrix with center i, j, with size 2p+1 by 2p+1.
 
Last edited:
  • #3


Hi Miyakooo,

To generate a matrix-shaped region of an image with a designated center, you can use the "imcrop" function in MATLAB. This function allows you to specify the coordinates of the center point and the dimensions of the region you want to crop. For example, if your image is stored in a variable called "image" and you want to crop a region of size 5x5 with the center at row 10 and column 15, you can use the following code:

cropped_region = imcrop(image, [15-2.5, 10-2.5, 5, 5]);

This will return a 5x5 matrix containing the pixels from the original image centered at row 10 and column 15. You can then repeat this process for multiple random points to sample different regions of the image. I hope this helps with your project! Good luck.
 

1. What is matrix sampling in Matlab?

Matrix sampling in Matlab refers to the process of selecting a subset of elements from a matrix. This can be done randomly or based on certain criteria, such as specific rows or columns.

2. How can I perform matrix sampling in Matlab?

There are several ways to perform matrix sampling in Matlab, such as using the "randperm" function to randomly select elements, or using indexing to select specific rows or columns.

3. Can I sample a matrix without replacement in Matlab?

Yes, you can use the "datasample" function in Matlab to sample a matrix without replacement. This function allows you to specify the number of samples and whether or not to replace the selected elements.

4. Is there a limit to the number of elements I can sample from a matrix in Matlab?

There is no specific limit to the number of elements you can sample from a matrix in Matlab. However, it is important to ensure that the sampled elements do not exceed the size of the original matrix.

5. Can I sample a matrix based on a specific condition in Matlab?

Yes, you can use conditional statements along with indexing to sample a matrix based on a specific condition in Matlab. This allows you to select elements that meet certain criteria, such as being greater than a certain value.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
862
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
746
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top