Indexing a matrix intuitively
5 views (last 30 days)
Show older comments
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?
0 Comments
Answers (3)
Matt J
on 12 May 2017
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 Comments
Matt J
on 12 May 2017
Edited: Matt J
on 12 May 2017
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
on 12 May 2017
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.
0 Comments
Steven Lord
on 12 May 2017
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.]
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!