Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
How to efficiently arrange a FOR-LOOP if we have two condition with different nValue
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (2)
Joshua
el 8 de Jul. de 2017
Editada: Joshua
el 8 de Jul. de 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
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!