Should I use primary or dependent property?

2 Ansichten (letzte 30 Tage)
J. Hu
J. Hu am 8 Okt. 2013
Kommentiert: J. Hu am 8 Okt. 2013
There is one property which by nature is dependent property because it is obtained from calculation using primary independent properties. The property is a 3*3 matrix which is obtained by calculation for each single element in a two-layer loop with 3 counts each - so total needed calculation is 3*3=9 for the matrix.
In another function, which does calculation in a two-layer loop with 3 counts each too, it needs to call the property within function. So the property needs to be called for 3*3 times.
I am not 100% sure how the calling of a dependent property works in Matlab. Based on my understanding, every time a dependent property is called, the full function to calculate the dependent will need to be ran fully. So even I just need to have one of the element values in the matrix, it still calculate all elements ones the dependent property is called. So the total calculation performed for dependent property matrix is 3^2 = 9 and the calculation for element is 3^2*3*3 = 81, of which actually 72 times calculation is just repeating. I am thinking this is the reason causing my code running slow.
So I am thinking of putting this dependent property as a primary independent property by doing pre-calculation in the class constructor. So all elements only need to be calculated once.
Is my understanding correct, my solution reasonable? Any options? Much appreciation.

Akzeptierte Antwort

per isakson
per isakson am 8 Okt. 2013
Bearbeitet: per isakson am 8 Okt. 2013
"Is my understanding correct" . Yes
"my solution reasonable" . Yes
"Any options?" . I don't think there is any better option.
Did you analyze your code with profile?

Weitere Antworten (1)

Matt J
Matt J am 8 Okt. 2013
Bearbeitet: Matt J am 8 Okt. 2013
Based on my understanding, every time a dependent property is called, the full function to calculate the dependent will need to be ran fully.
Only if you do that full calculation in the Dependent property's get() method. However, there are ways to do one-time calculations of dependent data while still using Dependent properties to sync it with independent data. Consider the class below. The independent data is an angle kept in theta_data and the dependent data is a corresponding 2x2 rotation matrix kept in R_data.
Note that any update of theta, triggers an update of the rotation matrix, thus keeping them in sync. Note also, though, that the only time the rotation matrix gets recalculated is when theta is changed.
classdef myclass
properties (Access=private)
R_data;
theta_data;
end
properties (Dependent)
theta
R
end
methods
function obj=myclass(theta)
obj.theta_data=theta;
obj.R_data=calcR(obj,theta);
end
function val=get.theta(obj)
val=obj.theta_data;
end
function obj=set.theta(obj,val)
theta=val;
obj.theta_data=theta;
obj.R_data=calcR(obj,theta);
end
function val=get.R(obj)
val=obj.R_data;
end
end
methods (Access=private)
function R=calcR(obj,theta) %normal method
R=[cosd(theta) -sind(theta); sind(theta) cosd(theta)];
end
end
end

Kategorien

Mehr zu Number games finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by