Hi guys, I need an help with an if, it's the first time I use it and I am not sure if it works because if I write just & a note appears suggesting to use &&, but if I put && turns an error: ??? Operands to the and && operators must be convertible to logical scalar values.
the code is
for t=2:time
dWsim(t,:)=mu0+sigma0.*rndnum(t,:);
Wsim(t,:)=Wsim(t-1,:)+dWsim(t-1,:);
B(t,:)=1./((m-1)*(1-Wsim(t,:))).^(1/(m-1));
sigma(t,:)=sigma0.*((B(t,:)./B(1,:)).^m);
mu(t,:)=(sigma(t,:).^2).*(m./2.*B(t,:))+mu0.*((B(t,:)./B(1,:)).^m);
if(uninum(t,1:N)>0.5)&(mu(t,:)*(1/k)>uninum(t,1:N));
B(t,:)=B(t,:).*(1-k);
else
B(t,:)=B(t,:);
end
end
Can you please explain me in simple word swhich is the difference between & and &&
Thanks in advance
Gaia

 채택된 답변

Jan
Jan 2011년 7월 27일

1 개 추천

The IF statenment is usually a scalar, otherwise an implicite ALL is performed. But perhaps you want an ANY:
if any(uninum(t,1:N)>0.5) && any(mu(t,:) / k > uninum(t,1:N))
or:
if all(uninum(t,1:N)>0.5 & mu(t,:) / k > uninum(t,1:N))
BTW: "* (1/k)" -> "/k" looks easier.

댓글 수: 1

gaia buratti
gaia buratti 2011년 7월 27일
thabks for the explanation na dfor the suggestion, it does not solve my problem but can be really useful in the future. Btw, thanks to you I understood the mistake, now seems working.
I know about the I was just copying a formula...

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2011년 7월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by