简体   繁体   中英

Select Diagonal Elements of a Matrix in MATLAB

Consider the following matrix in 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

I have to generate directional variograms for such 7 x 7 windows(moving) of an image. I will use nlfilter for the process but for developing the function to calculate variograms I am not able to decide how to select elements in the window. For example when I consider the central value 25, in EW direction I have to consider only 25, 26, 27 and 28; in NE direction I have to consider only 25, 19, 13 and 07 when the lag chosen is 1. Is there any standard command to do so?

You can also do it like this:

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

resulting in

v = [1 1 1 1 1]

You can write a function to get these elements yourself easily:

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))

Just write this code in a function (preferably an m-file), perform your operation and pass the result back.

The diag function returns the diagonal elements of a matrix (or returns a diagonal matrix when passed a vector).

This is Generic matrix solution (not for MATLAB) suppose matrix 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];

in this matrix we want to search continuously 3 times appearence of AA diagonally.

Solution:- step 1 for whole matrix we have to create 4 seperate for loops to search the appearence of AA continuously 3 times

在此输入图像描述

i am adding method through which a user can search all loop and can find the item.

 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

now you can call this method any where in class and can check in that matrix the searchable item is available or not in repeatedly order Horizontally, Vertically and diagonally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM