IF CONDITION NOT WORKING

7 vues (au cours des 30 derniers jours)
heir ancestors
heir ancestors le 23 Jan 2017
Modifié(e) : Niels le 27 Jan 2017
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

Réponses (1)

Niels
Niels le 23 Jan 2017
Modifié(e) : Niels le 23 Jan 2017
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 commentaires
heir ancestors
heir ancestors le 27 Jan 2017
Modifié(e) : heir ancestors le 27 Jan 2017
Thank you very much niels for your answers and for your time ..
Now , I know that the problem is in the condition if, overwriting problem : this is what is supposed to do : It is supposed to check if every element of a row is negative :
I will explain here with my real Gamma matrix :
8.2245 4.6785 -2.7385 -9.7237 -14.7297 -17.6314 -17.0755
13.5994 10.0534 2.6364 -4.3488 -9.3548 -12.2565 -11.7006
18.4996 14.9536 7.5366 0.5514 -4.4546 -7.3563 -6.8004
22.8070 19.2610 11.8440 4.8588 -0.1472 -3.0489 -2.4930
26.4971 22.9511 15.5341 8.5489 3.5430 0.6412 1.1971
29.6054 26.0593 18.6424 11.6572 6.6512 3.7494 4.3054
I want my if condition to check if the first element (8.2245)<0 ? it is not so : apply the second equation and calculate the appropriate value of Snna:
and then go to the second element 13.5994 , check if it is negative , it is not so apply the second equation also for positive numbers, here I want to the iteration line by line (Vertically) so the third element will be 18.4996 do the same thing and then 22.8070, 264971, 296054 and then go to the second column and check for 4.6785, 10.0534 ( one by one element) ...etc, when you will find a negativ element apply the first equation for negative numbers (one by one element)...
Hope I was clear explaining my problem, I'm really sorry, I didn't practice with matlab since my university courses ..thank you again niels for your time and efforts..
Niels
Niels le 27 Jan 2017
Modifié(e) : Niels le 27 Jan 2017
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?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Particle & Nuclear Physics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by