Select Diagonal Elements of a Matrix

7 visualizzazioni (ultimi 30 giorni)
Chethan S
Chethan S il 3 Apr 2011
Modificato: SS89 il 1 Gen 2021
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?

Risposta accettata

John D'Errico
John D'Errico il 3 Apr 2011
You need to learn how to index into a matrix using a single index. This is just the number of the element in terms of how it is stored in memory. MATLAB stores a 2-d array in order, going down the columns. So the elements in column 1 are the first 7 elements in memory, then come the elements of column 2, etc. So if you extracted elements [4 10 16] from the array, you would get
matrix([4 10 16])
ans =
22 16 10
We get this result simply enough by recognizing that the 4th element in memory is the (4,1) element, thus from the first column. If your matrix is NxM, then this computation is simply
singleindex = rowindex + (columnindex-1)*N
Read the help for sub2ind, if you don't want to bother doing the computations (they are trivial as you can see) yourself.

Più risposte (1)

SS89
SS89 il 1 Gen 2021
Modificato: SS89 il 1 Gen 2021
diagonal elements of matrix x:
Diagonal_elements=cumsum([0 repmat(size(x,1)+1,[1 size(x,2)-1])])+1;

Categorie

Scopri di più su Images in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by