# How to find the indices within blocks and sort the blocks thereafter?

3 views (last 30 days)
Xiaohan Du on 18 Jun 2018
Answered: Prajit T R on 21 Jun 2018
Hi all,
Say I have the following matrix contains 15 elements:
M =
6 2 9
7 2 3
9 3 10
10 9 4
6 3 2
and the following coordinates which represent 4 blocks:
K>> block{:}
ans =
-1 -1
0 -1
0 0
-1 0
ans =
-1 0
0 0
0 1
-1 1
ans =
0 -1
1 -1
1 0
0 0
ans =
0 0
1 0
1 1
0 1
Now I'd like to separate the elements in M into 4 blocks using information in 'block' as the following picture: So the result should be:
K>> mBlock{:}
ans =
6 2
7 2
9 3
ans =
2 9
2 3
3 10
ans =
9 3
10 9
6 3
ans =
3 10
9 4
3 2
How can I do it? Thanks a lot!

#### 1 Comment

Bob Nbob on 18 Jun 2018
What is the logic behind the block creation? Will they always be the same (four quadrants)?

Prajit T R on 21 Jun 2018
Hi
Here's a code which will work for a size m x n matrix provided that m and n are odd values. To incorporate even values, you have to make some minor tweaks, that's all. You can divide a matrix into 4 quadrants as per your requirement using the following:
M =[6 2 9; 7 2 3; 9 3 10; 10 9 4; 6 3 2]; %The input matrix that needs to be divided
m=5; %number of rows
n=3; %number of coloumns
a=floor(m+1/2); %finding the midline of the rows
b=floor(n+1/2); %finding the midline of the coloumns
Hope this helps
Prajit