Indexing a matrix intuitively

조회 수: 3 (최근 30일)
AJQ
AJQ 2017년 5월 12일
답변: Steven Lord 2017년 5월 12일
I am looking for a way to index a matrix so I can access elements like I would for a normal xy cartesian plane. For instance, M(1,1) would refer to the function value z = f(x,y) at the origin (0,0) of the XY plane. M(3,4) wouldn't refer to simply the element of M on the 3rd row and 4th column, rather the value of z at the point (x,y)=(3,4). I would only like to work with INTEGERS and only on the 1st quadrant for the sake of preserving regular rules of matrix indexing (no fractions or negative indices). Can this be done with matrices or do I need a different data type?

답변 (3개)

Matt J
Matt J 2017년 5월 12일
If you're excluding fractions and negative indices, then basically all you're saying is that you want matrices whose indexing starts at 0 instead of 1. You could look at my ZeroBased class, and if nothing else use it as a basis for what you'd like to do. However, I don't think it's worth it. I think you'll eventually accept that mapping spatial coordinates to 1-based indices yourself is the least painful way of doing things.
  댓글 수: 2
AJQ
AJQ 2017년 5월 12일
Transferring the origin is not all of it. Say I fixed the 0 indexing problem. Now say I have a 4*4 matrix containing values of z = f(x,y) at 16 grid points. Now if I want to access the value of z at (x,y)=(2,3) I can't just write M(2,3) because that would give me the value at the 3rd row 4th column, corresponding to (x,y)= (3,1)
Matt J
Matt J 2017년 5월 12일
편집: Matt J 2017년 5월 12일
I don't see how M(2,3) corresponds to (x,y)= (3,1) in your example, because you haven't said where in the 4x4 matrix you are placing the origin nor which way your axes are pointing.
My point, though, was that if you disallow negative indices then the origin (0,0) has to be placed at one of the corners of the matrix. Otherwise, you will have elements in the matrix with negative indices. It seems like it changes very little.

댓글을 달려면 로그인하십시오.


Guillaume
Guillaume 2017년 5월 12일
Well, if you really wanted the content of a matrix to map to your representation of a normal xy cartesian plane, then the row along the y origin would be the last row of the matrix, not the first. IE, m(0, 0) would be the last row, first column.
The indexing is matlab is what it is. If you want to map it to a cartesian plane, just picture your plane with the x axis pointing down, the y axis pointing right and the origin at (1, 1).
You could create your own class, even deriving with double, to change the indexing rules, e.g. as a starter
classdef cartesiandouble < double
methods
function this = cartesiandouble(m)
if nargin == 0
m = [];
end
this = this@double(m);
end
function val = subsref(this, S)
if S.type == '()'
assert(iscell(S.subs) && numel(S.subs) == 2, 'indexing not implemented');
m = double(this);
val = m(size(m, 1) - S.subs{2}, S.subs{1} + 1);
else
val = builtin('subsref', this, S);
end
end
end
end
Usage:
m = cartesiandouble(magic(5))
m(0,0)
m(4,0)
m(0,4)
m(4,4)
but in my opinion, the value of this is limited. Just accept the indexing for what it is.

Steven Lord
Steven Lord 2017년 5월 12일
So the rows and columns of the matrices you're using have have some sort of coordinate information associated with them in your application? That sounds like you want indexing to be more like gridded interpolation.
It probably wouldn't be that difficult to write a class that stores the data, the coordinates, and a griddedInterpolant and has customized parenthesis indexing that makes use of the stored griddedInterpolant to retrieve values at a specific coordinate. Indexed assignment, going from coordinates to indices in the stored matrix, may be a bit trickier but shouldn't be that difficult.
The toughest part of creating that object would be deciding what to do with mixed information. For instance, let's say you have a matrix M1 that represents the first quadrant (both coordinates non-negative) and another matrix M3 that represents the third quadrant (both coordinates non-positive.) What does A = M1+M3 return?
Combining the two matrices together into a larger grid (with queries for points in quadrants 2 and 4 returning NaN) seems reasonable, but what is the value of the origin A(0, 0) in this scenario if M1 and M3 have different data for the origin? Who wins? [Figuring out a consistent system for linear algebra will be even more fun for you.]

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by