Ouputting for loop into a matrix

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

Respuestas (2)

Vieniava
Vieniava el 8 de Mzo. de 2011
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 comentarios

Tom Starley
Tom Starley el 8 de Mzo. de 2011
Vieniava
This code is brilliant, I have changed the Ci and Cd to 0.1:0.1:3 as I am sure the values will lie inbetween 0 and 3. This also much reduces the computation time!
The only problem I still seem to be having is at the end, it displays the following:
The smallest time 95.005982 is for Ci=118.885774 and Cd=140.111798The smallest time 159.250258 is for Ci=176.603264 and Cd=192.338545The smallest time 207.112835 is for Ci=220.914477 and Cd=234.133907The smallest time 246.479911 is for Ci=258.407939 and Cd=269.568466The smallest time 280.314259 is for Ci=290.946989 and Cd=300.968033The smallest time 310.678542 is for Ci=320.079490 and Cd=329.371710The smallest time 338.355919 is for Ci=347.032742 and Cd=355.602727The smallest time 363.866357 is for Ci=372.024060 and Cd=379.776219The smallest time 387.623178 is for Ci=395.379177 and Cd=402.887473The smallest time 409.935795 is for Ci=417.364765 and Cd=424.389820The smallest time 0.700000 is for Ci=0.700000 and Cd=0.800000The smallest time 0.900000 is for Ci=1.000000 and Cd=1.000000The smallest time 1.100000 is for Ci=1.000000 and Cd=0.700000The smallest time 0.400000 is for Ci=0.200000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=1.900000 and Cd=2.000000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000The smallest time 0.100000 is for Ci=0.100000 and Cd=0.100000
Any ideas???
Tom Starley
Tom Starley el 8 de Mzo. de 2011
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 el 8 de Mzo. de 2011
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));

Iniciar sesión para comentar.

Paulo Silva
Paulo Silva el 8 de Mzo. de 2011

0 votos

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))

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 8 de Mzo. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by