-1
$\begingroup$

Consider the matrix 'm1' :

m1 = RandomReal[{0, 1}, {50, 50}];
MatrixForm@m1

The partition of this matrix into non-overlapping rectangles (arranged horizontally)

enter image description here

looks like this:

a=5;
b=10;
part1 = Partition[m1, {a, b}];

The question is how to part this area into rectangles with sides a, b, but arranged parallel to the diagonal (45 degree), that is, like this:

enter image description here

At the same time, those that are not a full rectangle must be neglected. One idea is to rotate the 'm1' matrix 45 degree and then use the command Partition[xxx, {a, b}]; and filter out 'full rectangles'.

$\endgroup$

1 Answer 1

4
$\begingroup$

There are some details that need to be filled in the question. Is the input matrix treated as 'image-like' or 'grid-like' when it comes to extraction? If it's 'grid-like' then the question is ill-posed because it depends on font-size of the matrix, spacing, how you'd treat numbers going over boundaries, etc.

If it is 'image-like' then non-axis aligned rectangles don't really exist and must have jagged edges. If you can accept that, then we can lean on Mathematica's image processing capabilities to produce a rotated mask from which we can extract the groups of pixels

m1 = RandomReal[{0, 1}, {50, 50}];
{a, b} = {5, 10};
chunksize = Ceiling[Dimensions[m1]/{a, b}];

(* Build a mask image of the grid *)
mask = ImageData[ImageAssemble[Partition[
     Table[
      Image[ConstantArray[i, Reverse@chunksize], "Bit16"], {i, 1, 
       a*b}], a
     ]], "Bit16"];

(* Use a bit16 image so you'll have enough labels. Use 
nearest-sampled so we don't create fractional interpolated labels *)
rotmask = 
  Round@ImageData@
    ImageRotate[Image@mask, -45 °, Reverse@Dimensions@m1, 
     Resampling -> "Nearest"];

(* Let's see what this looks like when rotated *)
Colorize@rotmask

(* Pick out the data in m1 corresponding to each value of our mask *)

parts = Association[
   Table[i -> Extract[m1, Position[rotmask, i]], {i, Min[rotmask] Max[rotmask]}]];

enter image description here

Each piece 'id' in the original partition (from top left to bottom right), now corresponds to the rotated piece in the mask. The parts association contains the pixel/matrix values that lie beneath it when superimposed on m1.

If you needed the mask to cover the edges and extend forever, like in your picture, then you could do something like this:

tmp = Image[i = 1; Table[i++, {x, 70}, {y, 70}]];
tmp = ImageResize[tmp, {10, 5}*ImageDimensions[tmp], 
   Resampling -> "Nearest"];
bigmaskrot = 
  Round@ImageData@
    ImageRotate[tmp, -45 °, Reverse@Dimensions@m1, 
     Resampling -> "Nearest"];
Colorize[bigmaskrot]

enter image description here

$\endgroup$
7
  • $\begingroup$ Thanks for the answer. My input is 'image-like'. For example: p1 = 0.25; p2 = 0.55; p3 = 0.15; p4 = 0.05; kprod [1] = {{p1, p2}, {p3, p4}}; kprod [n_]: = kprod [n] = KroneckerProduct [kprod [1], kprod [n - 1]]; number = 11; m1 = kprod [number]; It might look like this: MatrixPlot [m1, PlotTheme -> "Monochrome"] I am only interested in the numbers that are in each jagged rectangle. Now the question is how to extract only 'full jagged rectangles' (without jagged trapezoids and triangles) and numbers that are in their areas. $\endgroup$
    – ralph
    Feb 4, 2022 at 6:20
  • $\begingroup$ More specifically: ... and the original numbers from matrix 'm1' that are in their areas. $\endgroup$
    – ralph
    Feb 4, 2022 at 6:27
  • $\begingroup$ Probably such a selection can be done like this: in every 'full rectangle' there will be the same number of numbers? Is it true? If so, then in every other area (triangle or trapezium) there will be fewer numbers. So a simple use of 'Select []' is enough. $\endgroup$
    – ralph
    Feb 4, 2022 at 6:45
  • $\begingroup$ Thanks. Imagine translating the latter result 45 degrees to the left (the sides of the rectangles will be parallel / or perpendicular to the axis). How to get a list of these colored jagged rectangles each in the form of a matrix (where it is jagged, you have to insert a blank so that the matrix 'n x m' is always created)? $\endgroup$
    – ralph
    Feb 8, 2022 at 7:15
  • $\begingroup$ Of course, these matrices should contain the original numbers of the 'm1' matrix. $\endgroup$
    – ralph
    Feb 8, 2022 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.