Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 965
Chethan S. Avatar asked Apr 03 '11 06:04

Chethan S.


People also ask

How do you show the diagonal of a matrix in Matlab?

Description. D = diag( v ) returns a square diagonal matrix with vector v as the main diagonal. D = diag( v , k ) places vector v on the k th diagonal. k = 0 represents the main diagonal, k > 0 is above the main diagonal, and k < 0 is below the main diagonal.

How do you find the diagonal of a matrix?

Diagonal Elements of a MatrixAn element aij of a matrix A = [aij] is a diagonal elements of matrix if i = j, such as when rows and column suffixes are equal. Thus, a11 , a22 , a33, a44, … so on are diagonal elements of the matrix A = [aij]. The principal diagonal is also known as the leading diagonal.

How do you sort the diagonal elements of a matrix?

Approach: The idea is modify the selection sort to sort the diagonal elements of the matrix. Count of the diagonal elements of matrix M*N will be min(M, N). As we know the major diagonal elements of the matrix are mati,j where i == j. Therefore, the ith element of the major diagonal of the matrix will be mat[i][i].

How do you extract the values above the main diagonal from a square matrix?

Extracting the above values of the main diagonal in a matrixAn outer loop is used to iterate over the row elements and an inner loop is used to iterate over the column elements. And if the condition is used to check if the column number is greater than the row number. The element is then extracted from the matrix.


3 Answers

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

like image 41
Egon Avatar answered Oct 02 '22 15:10

Egon


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

enter image description here

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.

like image 43
Sangu Avatar answered Oct 02 '22 15:10

Sangu


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]
like image 82
ahven_ Avatar answered Oct 02 '22 16:10

ahven_