필터 지우기
필터 지우기

How to create a sparse vector of class objects

조회 수: 8 (최근 30일)
Simon
Simon 2013년 8월 26일
Dear all!
My aim is to write a class to handle locations in 3-d space (let's call it 'CoordinateClass'). Therefore I need to store the three coordinates (along with - at the moment - three other values) in an array. Each set of coordinate must have a unique ID. These IDs range from 0 to let's say 1e6 and are chosen by the user. The current solution is to use a sparse array. The coordinates are stored in the row corresponding to the ID.
What is the best way to get this scenario into a class? I could define a sparse array as the class property and work with that. But it is not as easy as to use a vector of objects like
C(1) = CoordinateClass([1 1 1 0 0 0]) % 3-d point at (1,1,1) with ID 1
C(100) = CoordinateClass([100 2 1 0 0 0]) % 3-d point at (100,2,1) with ID 100
The index is the user-defined ID. The problem is the memory usage. If I use large ID values all objects below are allocated as well. Is there a way to implement some kind of "sparse object" to decrease memory usage?
Thanks in advance

채택된 답변

Matt J
Matt J 2013년 8월 26일
편집: Matt J 2013년 8월 26일
You don't want to have a separate C(i) for each coordinate. That will allocate memory very inefficiently. You want a single object which holds your coordinate and other data as an array, e.g.,
classdef CoordinateClass
properties
coordinates;
IDs;
ThreeOtherValues;
end
end
and now you would have
C.coordinates=[1 1 1; 100 2 1; etc...]
C.IDs=[1;100; etc...]
C.ThreeOtherValues=[0 0 0; 0 0 0; etc...]
  댓글 수: 11
Matt J
Matt J 2013년 8월 30일
편집: Matt J 2013년 8월 30일
Why does C.cart2pol go through subsref and cart2pol(C) not?
Because C.cart2pol is an indexing expression and cart2pol(C) is not. I assume you're referring to situations when these expressions are executed outside the class. Inside a class method, neither expression goes through subsref.
Simon
Simon 2013년 8월 30일
Yes, your right, I execute the command from my test script. But inside the class the expression goes through subsref if I call it directly
sref = subsref(obj, s,)
(assumed the overloaded subsref handles this correctly) and not with
sref = builtin('subsref',obj,s);
That's ok.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sample Class Implementations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by