在matlab 如何取数组中固定范围的子矩阵

比如我想在500*500的数组中取3*3的矩阵
2024-12-02 10:04:29
推荐回答(1个)
回答(1):

the following is a simple example showing you that you have full freedom:
>> a=magic(5)

a =

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

>> b=a(1:3,1:3)

b =

17 24 1
23 5 7
4 6 13

>> b=a(1:3,[1 3 5])

b =

17 1 15
23 7 16
4 13 22

>> b=a([1 2 4],[1 3 5])

b =

17 1 15
23 7 16
10 19 3

you can even try:
>> c=randint(3,3,sum(size(a)))+1

c =

10 9 7
5 6 9
5 3 1

>> b=a(c)

b =

18 12 5
11 24 12
11 4 17

where you know matlab count element in matrix column by column, for example, in this 2D 5x5 matrix, a(10)=a(5,2)