matrix manipulating

2 次查看(过去 30 天)
PChoppala
PChoppala 2011-10-15
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

回答(1 个)

Walter Roberson
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);

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by