Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 30일)
balandong
balandong 2017년 7월 8일
마감: MATLAB Answer Bot 2021년 8월 20일
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

답변 (2개)

Joshua
Joshua 2017년 7월 8일
편집: Joshua 2017년 7월 8일
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 2017년 7월 9일
편집: balandong 2017년 7월 9일
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

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by