필터 지우기
필터 지우기

Ouputting for loop into a matrix

조회 수: 1 (최근 30일)
Tom Starley
Tom Starley 2011년 3월 8일
Hi guys
I have a function called 'simulator' that has two inputs, Cl and Cd. The function outputs a Time around a circuit for these inputs.
I want to be able to execute a loop that runs a combination of Cls and Cds between 0 and 3, and to output all the resulting times into a matrix, and THEN to be able to find the smallest time and Matlab to tell me what time this is, and what Cl and Cd combination this was.
Has anyone any idea how to go about this code? So far I have...
for i=[0.1:0.1:3]; j=[0.1:0.1:3]; a=[1:1:30]; y(a)= simulator([(i) (j)]) end
But I'll be honest, it doesn't really work at all...
Many thanks in advance guys!
Tom

답변 (2개)

Vieniava
Vieniava 2011년 3월 8일
Try this:
Ci=0.1:0.1:30;
Cd=0.1:0.1:30;
y=zeros(numel(Ci), numel(Cd));
for i=1:numel(Ci)
for j=1:numel(Cd)
y(i,j)=simulator([ Ci(i) Cd(j) ]);
end
end
imagesc(y)
[v i]=min(y);
[II JJ]=ind2sub(size(y), i);
sprintf('The smallest time %f is for Ci=%f and Cd=%f', v,Ci(II),Cd(JJ))
  댓글 수: 3
Tom Starley
Tom Starley 2011년 3월 8일
The problems being that it's printing loads of different times, and it's taking values for Cd and Ci in excess of 400 when I want all possible of combinations of Ci and Cd when Ci=[0.1:0.1:3] and Cd=[0.1:0.1:3]
Many thanks!
Jan
Jan 2011년 3월 8일
Simpler finding of minimal value using linear indexing:
[v, ind] = min(y(:));
sprintf('The smallest time %f is for Ci=%f and Cd=%f', v, Ci(ind), Cd(ind));

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


Paulo Silva
Paulo Silva 2011년 3월 8일
y=zeros(1,30);
i=[0.1:0.1:3];
j=[0.1:0.1:3];
for pos=1:numel(i)
y(pos)=simulator([i(pos) j(pos)])
end
fprintf('The minimum time was %d for i=%d and j=%d',min(y),i(y==min(y)),j(y==min(y))

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by