在MATLAB中选择矩阵的对角元素

时间:2011-04-03 06:52:46

标签: image-processing matlab

考虑MATLAB中的以下矩阵:

01 02 03 04 05 06 07

08 09 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31 32 33 34 35

36 37 38 39 40 41 42

43 44 45 46 47 48 49

我必须为图像的7 x 7窗口(移动)生成方向变异图。我将使用nlfilter进行处理,但是为了开发计算变量图的函数,我无法决定如何在窗口中选择元素。例如,当我考虑中心值25时,在EW方向上我只需要考虑25,26,27和28;在NE方向上,当选择的延迟为1时,我只需要考虑25,19,13和07.是否有任何标准命令可以这样做?

3 个答案:

答案 0 :(得分:8)

你也可以这样做:

A = eye(5);
v = A(1:size(A,1)+1:end);

导致

v = [1 1 1 1 1]

答案 1 :(得分:3)

您可以编写一个函数来轻松获取这些元素:

A = [01 02 03 04 05 06 07
     08 09 10 11 12 13 14
     15 16 17 18 19 20 21
     22 23 24 25 26 27 28
     29 30 31 32 33 34 35
     36 37 38 39 40 41 42
     43 44 45 46 47 48 49];

c = (size(A)+1)/2;
EW = A(c(1),c(2):end)
NE = diag(A(c(1):-1:1,c(2):end))

只需将此代码写入函数(最好是m文件),执行操作并将结果传回。

diag函数返回矩阵的对角元素(或传递向量时返回对角矩阵)。

答案 2 :(得分:2)

这是通用矩阵解决方案(不适用于MATLAB) 假设矩阵AxB =

[01 AA 03 04 05 06 07
 08 09 AA 11 12 13 AA
 AA 16 17 AA 19 AA 21
 22 AA 24 25 AA 27 28
 AA 30 AA 32 33 34 35
 36 AA 38 AA 40 41 42
 43 44 AA 46 AA 48 49];

在这个矩阵中,我们希望连续3次搜索AA对角线。

解决方案: - 步骤1 对于整个矩阵,我们必须创建4个单独的循环来连续3次搜索AA的外观

enter image description here

我正在添加一个方法,用户可以通过该方法搜索所有循环并找到该项目。

 local function check_win( matrx_table)

local counter = 1
local term = "AA"
local D = 1

-- for horizontal check for win---
for i = 1, no_rows, 1 do
    for j= 1, no_Columns, 1 do
        if((j+1) <= no_Columns) then             
            if(table_mXn[i][j] == term and table_mXn[i][j+1] == term)then
                counter = counter + 1;
            else
                counter = 1
            end               
            if(counter == 3)then
                return counter
            end               
        end
    end             
end

counter = 1
-- for vertical check for win--
for i = 1, no_Columns, 1 do
    for j= no_rows, 1, -1 do
        if((j-1) >= 1) then             
            if(table_mXn[j][i] == term and table_mXn[j-1][i] == term)then
                counter = counter + 1;
            else
                counter = 1
            end    
            if(counter == 3)then
                return counter
            end               
        end
    end             
end

counter = 1
D = 1
-- for diagonol  left half check for win in figure loop 1--
for m = 1, no_rows, 1 do
    D = 1
    for i =m, no_rows,1 do
        if(i+1 <= no_rows and D+1 <= no_Columns)then
            if(table_mXn[i][D] == term and table_mXn[i+1][D+1] == term)then
                counter = counter + 1;
                print("hhhhh")
            else
                counter = 1
            end               
            if(counter == 3)then
                return counter
            end     
            D = D + 1
        end
    end
end

counter = 1
D = 1
-- for diagonol  right half check for win in figure loop 2--
for m = 1, no_rows, 1 do
    D = m
    for i =1, no_rows,1 do
        if(i+1 <= no_rows and D+1 <= no_Columns)then
            if(table_mXn[i][D] == term and table_mXn[i+1][D+1] == term)then
                counter = counter + 1;
                print("hhhhh")
            else
                counter = 1
            end               
            if(counter == 3)then
                return counter
            end     
            D = D + 1
        end
    end
end

counter = 1
D = 1
-- for diagonol  left half check for win in figure loop 3--
for m = 1, no_rows, 1 do
    D = no_Columns
    for i =m, no_rows,1 do
        if(i+1 <= no_rows and D-1 >= 1)then
            if(table_mXn[i][D] == term and table_mXn[i+1][D-1] == term)then
                counter = counter + 1;
                print("hhhhh")
            else
                counter = 1
            end               
            if(counter == 3)then
                return counter
            end     
            D = D - 1
        end
    end
end

counter = 1
D = 1
-- for diagonol  left half check for win in figure loop 4--
for m = no_Columns, 1, -1 do
    D = m
    for i =1, no_rows,1 do
        if(i+1 <= no_rows and D-1 >= 1)then
            if(table_mXn[i][D] == term and table_mXn[i+1][D-1] == term)then
                counter = counter + 1;
                print("hhhhh")
            else
                counter = 1
            end               
            if(counter == 3)then
                return counter
            end     
            D = D - 1
        end
    end
end

end

现在你可以在课堂上的任何地方调用这个方法,并且可以在该矩阵中检查可搜索项目是否可用,水平,垂直和对角重复顺序。