matrix manipulating
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi there Peculiar problem to me.
I have a 3x3 matrix
w =
1 2 3
4 5 6
7 8 9
and x =
1.2000 2.6000
I want to access the values of the matrix at the locations given by 'x'.
So I use
round(x), and get x=(1,3).
The value of the matrix at 'x' is '7'.
Now, I want values in the matrix which are 1 cell adjacent to 'x', including 'x'. So that would be
(1,3), (2,3),(3,3), (1,2),(1,1)
and multiply them all.
and give a single output of the product.
Kindly help, mates!
Thanks
댓글 수: 0
답변 (1개)
Walter Roberson
2011년 10월 15일
x at location (1,3) is 3, not 7. x(3,1) is the one which is 7.
Array indexing is row first and then column. Arrays are stored internally in memory by going down columns. The internal order of the array you show would be 1, 4, 7, 2, 5, 8, 3, 6, 9.
Anyhow, I cannot see any way that the positions you list could be considered "adjacent" unless you are wrapping around in both the horizontal and vertical directions.
If you want horizontal and vertical wrapping, then:
Let R be the number of rows and C be the number of columns. Let x be the row number and y the column number for the position to work relative to. Then the positions you want are:
X = 1+mod([x;x-1;x+1;x;x]-1,R);
Y = 1+mod([y,y,y,y-1,y+1]-1,C);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!