Creating a pointer that behaves like a matrix

2 views (last 30 days)
Val Schmidt
Val Schmidt on 7 Jan 2014
Edited: Matt J on 8 Jan 2014
I would like to create a new object that inherits from the handle class which will give me the functionality of a pointer but retains the native behavior of a double (save the pointing part).
I've started by creating a class that looks like this, over-riding the subsref() ad subsasgn() functions to provide basic indexing functionality:
classdef p < handle
properties:
Data
end
methods
function obj = subsasgn(obj,theStruct,Value)
obj.Data(theStruct.theSubs{:}) = Value;
end
function Value = subsref(obj,theStruct)
Value = obj.Data(theStruct.subs{:});
end
end
end
This works and I can do the following:
d = p();
d(1:3) = 1:3;
q = d;
q(3)
ans:
3
With this setup, I get most of what I want but, since nearly every math and function operation requires a double, it seems I have to overload them all to make things like the following work:
u = p();
u(1:3) = 1:3;
v = p();
v(1:3) = 1:3;
u.*v % requires overloading.
sum(u) % requires overloading.
% etc...
I was wondering if there's another way to do this, or maybe another class to inherit from (too bad I can't inherit from double) that might simplify this.

Answers (1)

Matt J
Matt J on 8 Jan 2014
Edited: Matt J on 8 Jan 2014
Is it really worth it? What pointer-like operations are you hoping to do with the class?
If you do have compelling need for handle semantics, it would probably be easier (even if uglier) just to operate on the Data property explicitly,
u=p();
u.Data=1:3;
v=u;
v.Data(1)=sum(v.Data);
>> u.Data,
ans =
6 2 3
  2 Comments
Matt J
Matt J on 8 Jan 2014
Val Schmidt commented:
These are fair questions.
I think what you've suggested would produce a copy operation when the data is passed to a function, even if the parent object is passed instead and that's what I was hoping to avoid. But it seems to me that even my method will produce a copy operation in the call to subsref().
I have a need (well, I think I do) to have the same data show up in different data structures for different purposes. It's large chunks that one would not want to copy if possible, for subsequent calculations. So I was thinking to try to do this with handle classes to generate a kind of pointer to a matrix from several places.
Maybe I should rethink my strategy.
Matt J
Matt J on 8 Jan 2014
Edited: Matt J on 8 Jan 2014
No, no copies should be created, unless you deliberately make one. If you only make in-place changes to u.Data or v.Data, like in the example below, then I don't see where a deep copy would be generated.
u=myclass;
u.Data=1:5;
v=u;
change(v);
UData = u.Data,
This produces,
UData =
100 2 3 4 5
%%%%%%%%%%%%%%%%%%
function change(v)
v.Data(1)=100;
end
classdef myclass < handle
properties
Data
end
end

Sign in to comment.

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!