How to group the corresponding elements of an array?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I'm calculating the value of a parameter f_x whose value changes according to the following conditions.
I haven't shown the equation for all the parameters here, as this would be enough to give an idea on how f_x is calculated and how it's value is changing according to the above conditions.
Now my question is how will I calculate f_x by grouping all the 5 parameters values separately for dry, wet, low and very low conditions.
For eg.,I need to give something like this,
f_x.dry= F_x/Q;
f_x.wet=F_x/Q;
where f_x gives me a value on considering the dry condition data. (Likewise for other conditions). So how would I need to define the conditions, so that I can give f_x.dry
Any help would be greatly appreciated.
Thanks
6 comentarios
Joseph Cheng
el 16 de Jun. de 2014
Editada: Joseph Cheng
el 16 de Jun. de 2014
I agree with Sara's 4th point. x^3/2 is not to the 3/2 power is actually (x^3)/2. However i learned something today. [] does work as it should in math notation. However i can see the downsides of interchanging its useage as it could be confused with array concatenation.
in summary are you attempting to run this 4 times for each of the conditions? where the end results will give you either f_x.dry or f_x.(condition) with the parameters corresponding to the condition variable? if so why not just use a for loop to loop when condition=1:4 and a switch case statement based on condition.
for condition = 1:4
k_A = k_A_index(condition )
k_S = k_S_index(condition )
cof_0 = cof_0_index(condition )
A = A_index(condition )
B = B_index(condition )
%%do processing stuff here
switch condition
case 1
f_x.dry = ________________
case 2
fx_x......
end
end
Respuestas (2)
dpb
el 10 de Jun. de 2014
If you have a recent release, put the coefficient data into a table object...
If not, use a named structure or a named index variable into an array. Syntax wouldn't be quite so clean but would accomplish the deed.
dpb
el 17 de Jun. de 2014
Well, I don't have the table object here so can't; but without it the outline of what I'd do is sotoo...
%Parameters Dry wet Low very low
k_A=[1.00 1.00 1.00 1.00];
k_S=[0.40 0.40 0.40 0.40];
cof=[0.55 0.30 0.06 0.03];
A=[0.40 0.40 0.40 0.40];
B=[0.60 0.20 0.20 0.10];
V_wx=40;
V_rx=20;
V_wy=21;
V_ry=20;
V_f=(V_wx+V_rx)/2;
s_x=(V_wx-V_rx)/V_f;
s_y=(V_wy-V_ry)/V_f;
s=sqrt(s_x^2+s_y^2);
w_x=V_wx-V_rx;
w_y=V_wy-V_ry;
w=w_x+w_y;
c=@(ix) cof(ix).*((1-A(ix)).*exp(-B(ix).*w)+A(ix));
ix=1:length(k_A); ix=ix(:);
[ix c(ix).']
Here I've revamped your data tables to shorten them to the base names with the intent of using a value directly from the array instead of thru some other intermediary variable ("the Matlab way"). Then I recast the initial calculation of the coefficient variable cof to be an anonymous function c that can be invoked with either a single value or a vector of values of the various pieces that make it up.
Lastly, to demonstrate its use, I set the index vector to the possible choices and invoked the function thru the variable containing it and displayed the input index and the result for each at the command line.
As far as the desire for a name associated with the condition that could be used, you could do something like
params={'Dry' 'Wet' 'Low' 'Very Low'}.';
ix=listdlg('PromptString','Select a condition:',...
'SelectionMode','single',...
'ListString',params)
at the beginning to select a particular index w/ user interaction.
From here it shouldn't take too much to recast the rest of your code in a similar vein.
0 comentarios
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!