Select Diagonal Elements of a Matrix

조회 수: 7 (최근 30일)
Chethan S
Chethan S 2011년 4월 3일
편집: SS89 2021년 1월 1일
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?

채택된 답변

John D'Errico
John D'Errico 2011년 4월 3일
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.

추가 답변 (1개)

SS89
SS89 2021년 1월 1일
편집: SS89 2021년 1월 1일
diagonal elements of matrix x:
Diagonal_elements=cumsum([0 repmat(size(x,1)+1,[1 size(x,2)-1])])+1;

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by