Select Diagonal Elements of a Matrix
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chethan S
am 3 Apr. 2011
Bearbeitet: SS89
am 1 Jan. 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?
0 Kommentare
Akzeptierte Antwort
John D'Errico
am 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.
0 Kommentare
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!