필터 지우기
필터 지우기

How to use MESHGRID or NDGRID instead of multiple FOR-Loop?

조회 수: 2 (최근 30일)
balandong
balandong 2017년 11월 15일
편집: balandong 2017년 11월 16일
Dear coder, Briefly, the program objective was to evaluate all possible constant pairs for a different set of condition. Since there are SIX constant, the current implementation required SIX nested FOR-loop. However, the code looks so messy.
for f_p_kdeA=s.p_kdeA
for f_p_kdeF=s.p_kdeF
for f_p_PrmSq00=s.p_PrmSq00
for f_p_PrmSq01=s.p_PrmSq01
for f_p_PrmSq10=s.p_PrmSq10
for f_p_PrmSq11=s.p_PrmSq11
s=eval_s(s);
end
end
end
end
end
end
Thus, I wonder how to make the following code to be more efficient by eliminating the FOR-loops. Thru some reading, using the NDGRID is a possible ways to make the code more efficient. However, I have limited knowledge on how to implement it. I really appreciate if someone can point out on how to do it.
The complete code is a below
[comb,s]=ini();
comb= eval_forLoop(comb,s);
result=array2table(comb);
result.Properties.VariableNames=s.HeaderName;
function [comb,s]=ini()
load('stt_table.mat')
data=table2array(stt_table);
[s.state.kde,s.state.prm,s.state.sq]=deal(data(:,1),data(:,2),data(:,3));
s.p_kdeA=0:0.5:1;
s.p_kdeF=0:0.5:1;
s.p_PrmSq00=0:0.5:1;
s.p_PrmSq01=0:0.5:1;
s.p_PrmSq10=0:0.5:1;
s.p_PrmSq11=0:0.5:1;
nRow=(numel(s.p_kdeA))*(numel(s.p_kdeF))*(numel(s.p_PrmSq00))*...
(numel(s.p_PrmSq01))*(numel(s.p_PrmSq10))*(numel(s.p_PrmSq11));
comb=nan(nRow,7);
end
function comb= eval_forLoop(comb,s)
c_d=1;
for f_p_kdeA=s.p_kdeA
for f_p_kdeF=s.p_kdeF
for f_p_PrmSq00=s.p_PrmSq00
for f_p_PrmSq01=s.p_PrmSq01
for f_p_PrmSq10=s.p_PrmSq10
for f_p_PrmSq11=s.p_PrmSq11
s.table1=[f_p_kdeA;f_p_kdeF];
s.table2=[f_p_PrmSq00;f_p_PrmSq01;f_p_PrmSq10;f_p_PrmSq11];
s=eval_s(s);
comb(c_d,:)= [s.avrge_aa;f_p_kdeA;f_p_kdeF;f_p_PrmSq00;...
f_p_PrmSq01; f_p_PrmSq10;f_p_PrmSq11];
c_d=c_d+1;
end
end
end
end
end
end
end
%

채택된 답변

balandong
balandong 2017년 11월 16일
Credit to KSSV & Andrei Bobrov, their proposed idea lead to the solution below.
KSSV: His idea allow more flexibility for the spacing interval.
Andrei Bobrov: The end result of the a = [a{:}]; is what desired.
Proposed solution
s.p_kdeA=0:0.2:1;
s.p_kdeF=0:0.5:1;
s.p_PrmSq00=0:0.5:1;
s.p_PrmSq01=0:0.5:1;
s.p_PrmSq10=0:0.02:1;
s.p_PrmSq11=0:0.5:1;
S = struct2cell(s) ;
CpTable = cell(6,1);
[CpTable{end:-1:1}] = ndgrid(S{:}) ;
CpTable = cellfun(@(x)x(:),CpTable,'un',0);
CpTable = [CpTable{:}];

추가 답변 (2개)

KSSV
KSSV 2017년 11월 15일
s.p_kdeA=0:0.5:1;
s.p_kdeF=0:0.5:1;
s.p_PrmSq00=0:0.5:1;
s.p_PrmSq01=0:0.5:1;
s.p_PrmSq10=0:0.5:1;
s.p_PrmSq11=0:0.5:1;
S = struct2cell(s) ;
[I{1:numel(S)}] = ndgrid(S{:}) ;
I
  댓글 수: 1
balandong
balandong 2017년 11월 16일
편집: balandong 2017년 11월 16일
Thanks for your answer, it really compact, really appreciate it.

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2017년 11월 15일
a = cell(6,1);
[a{end:-1:1}] = ndgrid(0:.5:1);
a = cellfun(@(x)x(:),a,'un',0);
a = [a{:}];
And rewrite your functions eval_s and etc for new variable a!
  댓글 수: 1
balandong
balandong 2017년 11월 16일
Thanks for the fast response. Your solution work like a charm, really appreciate it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by