IF CONDITION NOT WORKING
7 次查看(过去 30 天)
显示 更早的评论
Hello every one :
I have a serious problem with matlab ...
Gamma3 is a 6*7 matrix , h11 also , where I have positive and negative values
I want to calculate the following code
[e1,o1]=size(Gamma3)
ja1=1:e1-1
ha1=2:e1
[r1,r2]=size(h22)
xa1=1:r1-1
if Gamma3>0
Snna=w.*sind(Gamma3(ja1,:)).*tand((2*Gamma3(ha1,:))+(90-h22(xa1,:))+0.26);
else
Snna=(w*sind(abs(Gamma3(ja1,:))))./(tand(h22(xa1,:)));
end
the equations are working great without the loop if, but when I put the loop if, it's calculation only the seconde condition (else) which means when (Gamma3<0)
please help me to fix this ..
thank you
0 个评论
回答(1 个)
Niels
2017-1-23
编辑:Niels
2017-1-23
you should add what your code is supposed to do (especially what snna is/ when shall the condition be fullfilled)
if you compare a matrix to a scalar, this is what you get:
Gamma3=randn(6,7) % some random matrix (not yours)
>> Gamma3>0
ans =
6×7 logical array
1 0 1 1 1 1 1
1 1 0 1 1 0 0
0 1 1 1 1 0 1
1 1 0 0 0 0 0
1 0 0 1 1 0 0
0 1 1 1 0 1 0
every element of Gamma3 is compared to 0 and the whole expression is true if every element is >0 .
depending on what you want you may have to use a loop
7 个评论
Niels
2017-1-27
编辑:Niels
2017-1-27
so you just check the first element of each row, ok
then the code i posted should do what you need, did you test it?
for i=1:e1-1 % enter each row, last one cant be entered
% since you would get an error, cause tand((2*Gamma3(i+1,:)... enters i+1 row (which would be the 7th if i=6) and this one does not exist
% maybe you have to set the indices yourself cause you know what your algorythm should do with each row of gamma3
if Gamma3(i,1)<0 % check if first entry is negativ
Snna(i,:)=w.*sind(Gamma3(i,:)).*tand((2*Gamma3(i+1,:))+(90-h22(i,:))+0.26); % still difficult to guess what is actually meant by ha1, xa1 and so on
else
Snna(i,:)=(w*sind(abs(Gamma3(i,:))))./(tand(h22(i,:)));
end
end
edit: the way you wrote your code in your question, shouldnt Snna be a 5x7 matrix?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!