Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How to efficiently arrange a FOR-LOOP if we have two condition with different nValue

1 vue (au cours des 30 derniers jours)
balandong
balandong le 8 Juil 2017
Clôturé : MATLAB Answer Bot le 20 Août 2021
Dear Matlab coder,
The objective is to execute a function depending to the three different variable state (i.e., Condition_Cond, Condition_TTDay & CONDITION_3). The function will find the index location if it fulfill the 3 conditions. State of each of the 3 condition are: Condition_Cond: Acond & Bcond; Condition_TTDay: BL, SS1 & SS2:. However, the state of the CONDITION_3 is depending to the Condition_TTDay. In other word, CONDITION_3 will have 2 or 3 state if the Condition_TTDay is either BL or (SS1 & SS2).
To realize this, a 3 level FOR-LOOP is constructed. Since the number of execution times at the most inner FOR-LOOP (i.e., CONDITION_3) is dependent on the second level FOR-LOOP (i.e., Condition_TTDay), a SWITCH is introduced. The following code and expected figure are shown below. The excel file can be download thru this link excelFile
My question is, how can I avoid the usage of SWITCH, is there any other alternative to make this code more compact? In addition, it is difficult to assign the output of Valindex, since the inner loop (Condition3) have two different values!.
Thanks in advance for the wisdom
filename = 'DataExcel.xlsx';
[~,~,raw] = xlsread(filename,'Sheet1','','basic');
Condition_Cond = {'ACond' 'BCond'};
Condition_TTDay = {'BL' 'SS1' 'SS2'};
c= 1;
for i=1:2
for j =1:3
condition_1 = Condition_Cond {i};
condition_2 = Condition_TTDay {j};
switch condition_2
case 'BL'
for k =1:2
condition_3 = k;
Valindex (:,c) = calMean (raw,condition_1, condition_2, condition_3)
c=c+1
end
otherwise
for k =1:3
condition_3 = k;
Valindex (:,c) = calMean (raw, condition_1, condition_2, condition_3)
c=c+1
end
end
end
end
function Valindex = calMean (raw,condition_1, condition_2, condition_3)
valid = find(cellfun('isclass', raw(:, 2), 'char') & ...
cellfun('isclass', raw(:, 3), 'char') & ...
cellfun('isclass', raw(:, 7),'double'));
Valindex = valid((strcmp(raw(valid,2), condition_1)) & ...
(strcmp(raw(valid,3), condition_2)) & ...
([raw{valid,7}].' == condition_3));
end

Réponses (2)

Joshua
Joshua le 8 Juil 2017
Modifié(e) : Joshua le 8 Juil 2017
I think this answers your first question at least. Might take care of the second question as well since there is just one loop now.
filename = 'DataExcel.xlsx';
[~,~,raw] = xlsread(filename,'Sheet1','','basic');
Condition_Cond = {'ACond' 'BCond'};
Condition_TTDay = {'BL' 'SS1' 'SS2'};
c= 1;
for i=1:2
for j =1:3
condition_1 = Condition_Cond {i};
condition_2 = Condition_TTDay {j};
for k =1:length(condition_2)
condition_3 = k;
Valindex (:,c) = calMean (raw,condition_1, condition_2, condition_3)
c=c+1
end
end
end

balandong
balandong le 9 Juil 2017
Modifié(e) : balandong le 9 Juil 2017
Hi Joshua, Thanks for the response. I am afraid your solution does not produce the intended result. Some of the index is missing. In additio, the proposed solution cannot be generalized if, say, the condition_TTday have more than 3 possible state, for example Condition_TTday = 'BL' 'SS1'' SS2' 'SS3' 'SS4'.
Thanks for the response

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by