Borrar filtros
Borrar filtros

Using the find function in a loop

1 visualización (últimos 30 días)
Deanna
Deanna el 13 de Abr. de 2011
Can the find function be used in a loop? I have not been able to get it to work. I have tried many ways but the find command doesn't seem to like "i" in the statement. Here are two different ways I have tried it:
%Inititalize variables (P1 is a vector generated earlier in the program)
P1_int(1)=1;
i=1;
c=(1:length(P1)-1);
intdurl(1)=intdur;
for i=2:21 P1_int(i)=find((ttot(c)<intdurl(i-1)) & (ttot(c+1)>intdurl(i-1))); % determines i when ttot is intdurl(i-1)
intdurl(i)=intdurl(i)+intdur;
end
I have also tried:
%Inititalize variables (P1 is a vector generated earlier in the program)
P1_int(1)=1;
i=1;
c=(1:length(P1)-1);
intdur=5;
for i=2:21 P1_int(i)=find((ttot(c)<intdur*(i-1) & (ttot(c+1)>intdur*(i-1))); % determines i when ttot is intdur*(i-1)
end
Thanks!

Respuesta aceptada

the cyclist
the cyclist el 13 de Abr. de 2011
Your code fails when i=21, because the second condition [(ttot1min(c+1)>intdur*(i-1))] is never met, resulting in the find command giving an empty array.
I discovered this by first outputing the value of i, to see what iteration it crashed on, then using a conditional breakpoint at i==21, and poking into the pieces of the right-hand side.

Más respuestas (2)

Matt Fig
Matt Fig el 13 de Abr. de 2011
Is it possible that your find command is returning multiple results? If so, putting multiple results into a single element of an array will not work. Of course it would help if you showed the error message...

the cyclist
the cyclist el 13 de Abr. de 2011
The find command can certainly be used in a loop. Here are a few suggestions:
  1. Your code is not executable as written. It would be really handy if you could fill in values for the undefined variables, such that the code will run and exhibit the problem.
  2. Tell us the error or warning you are getting.
  3. The find command might be giving a single scalar value, or a vector, or an empty array as output. Can your code handle all that, as needed?
  4. Try breakpointing your code at the line where the find command is called, and see if anything suspicious is happening, or if you can manually run some sections of that line to see if it gives what you suspect.
  2 comentarios
Deanna
Deanna el 13 de Abr. de 2011
Hi. I can run the code (for the first iteration) outside of the loop and it works. When I try to run it with a loop I get the following error message:
??? Improper assignment with rectangular empty matrix.
Error in ==> m041311 at 94
P1_int(i)=find((ttot1min(c)<intdur*(i-1))&(ttot1min(c+1)>intdur*(i-1)));%determines
i when ttot1 is (i-1)*intdur
Here is all of my code:
clear all
% constants
Dw=7.6E-6;
rho=1.1;
Dose = 2.5/1000; % (g)
S=0.068/1000; % saturated solubility in g/cm^3
hc=30/10000; % critical particle radius in cm
ao1=40/10000; % initial radius (cm)
D1=Dose;
%
%Determine number of particles
Nm1=3*D1/((4*3.14159*ao1^3*rho));
%
%Determine initial particle weight
wo1=D1/Nm1;
%
wstep = 1000; % number of weight increments to evaluate
%
% INITIALIZATIONS
%
% Particle radius
a1(1)=ao1;
% Diffusion layer thickness
if a1(1)<hc
h1(1)=a1(1);
else
h1(1)=hc;
end
% Particle weight
w1(1)=wo1;
% Time
t1(1)=0;
ttot1(1)=0;
%
k=1;
% LOOP
%
for k = 2:wstep
% size bin 1
w1(k) = wo1-k*wo1/wstep;
c1(k) = Dw*S/rho/h1(k-1);
c2(k) = (3*w1(k-1)/(4*3.14159*rho))^(1/3);
c3(k) = (3*w1(k)/(4*3.14159*rho))^(1/3);
t1(k)= (c2(k)-c3(k)-h1(k-1)*log((h1(k-1) + c2(k))/(h1(k-1) + c3(k))))/c1(k);
a1(k)=(3*(w1(k)/rho)/(4*3.14159))^(1/3);
W1(k)=w1(k)*Nm1;
M1(k)=D1-W1(k);
P1(k)=M1(k)/D1*100;
ttot1(k)=ttot1(k-1)+t1(k); % total cumulative time up to kth iteration
ttot1min(k)=ttot1(k)/60;
aoh_1(k)=a1(k)/h1(k-1)*100;
if a1(k)<hc
h1(k)=a1(k);
else
h1(k)=hc;
end
end
%
%Display time for complete dissolution
fprintf('Time for complete dissolution is %3.2f min\n', ttot1min(wstep))
%
%define evaluation interval duration for next loop
intdur=(ttot1min(wstep))/20;
%
%Try find function code outside of a loop
%c=(1:length(P1)-1);
%P1_int=find((ttot1min(c)<1*intdur*1)&(ttot1min(c+1)>1*intdur*1));
%fprintf('Interval duration is %3.2f\n', (ttot1min(wstep)/20))
%fprintf('Interation no. for last step is %3.2f\n', P1_int)
%fprintf('Following value should equal interval duration %3.2f\n', ttot1min(P1_int))
%fprintf('Total percent diss at that interval is %3.2f\n', P1(P1_int))
%
%Inititalize variables
P1_int(1)=1;
MT(1)=M1(P1_int(1));
TT(1)=0;
i=1;
c=(1:length(P1)-1);
%
%Use find command in a loop
for i=2:21
P1_int(i)=find((ttot1min(c)<intdur*(i-1))&(ttot1min(c+1)>intdur*(i-1)));%determines i when ttot1 is (i-1)*intdur
MT(i)=M1(P1_int(i));%calculates total mass at each interval
PT(i)=MT(i)/Dose*100;%calculates total percent dissolved at each interval
TT(i)=(i-1)*intdur;
end
Deanna
Deanna el 13 de Abr. de 2011
Thanks so much!

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by