IF CONDITION NOT WORKING
Mostrar comentarios más antiguos
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
Respuestas (1)
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 comentarios
heir ancestors
el 23 de En. de 2017
Niels
el 24 de En. de 2017
for i=1:6*7 % number of elements (could use -numel- here as well or 2 for loops / one for rows, another one for colums)
if Gamma3(i)>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,:))); % dont know what w is but above you used a dot for multiplication so w may be no scalar, i added a dot
end
end
but i see no sense in using a loop here or comparing each element to 0 since you will overwrite Snna in each iteration so that you could jump right to the last one and just compare Gamma3(42) to 0. thats why i asked for the purpose of your code
Walter Roberson
el 24 de En. de 2017
Your condition is not well defined.
Suppose Gamma3(1,1) is > 0 but Gamma3(2,1) is not.
Then in the sub-expression Gamma3(ja1,:) in the first column of the first row, because ja1 starts from 1, the rule for Gamma3 > 0 has to be used. But in the sub-expression Gamma3(ha1,:) because ha1 starts from 2, the first position of the first row that results, the entry would be Gamma3(2,1) which we supposed is < 0, so the second rule has to be used for that location.
You have four situations that you need to define the behavior of. Consider any given offset of Gamma3(ja1,:), and the same offset into Gamma3(ha1,:) . Both of the entries might be > 0, or the first might be but not the second, or the first might be < 0 but the second might be > 0, or both might be < 0.
heir ancestors
el 26 de En. de 2017
Editada: heir ancestors
el 26 de En. de 2017
horrible aligning...
some questions:
1. why do you need 4 for loops?? if you want to set each entry of Snna to a certain value, 2 for loops are enough, can even be done with 1.
2. there is no A in your code
3. i can see 4 for loops and 1 if statement => 5 end, so why are there 7??
this code could probably do what you want: (since it is still not clear nobody can know)
% i set
ha1 = ja1+1;
% and
xa1=ja1;
for i=1:e1 % enter each row
if Gamma3(i,1)<0 % check if entry is negativ
% think again about your condition, you didnt tell as what exactly it should be,
%if you want to set the entire row to something, what do you compare here?? the first element of each row? i told you before that you are overwriting your Snna with each iteration...
Snna(i,:)=w.*sind(Gamma3(i,:)).*tand((2*Gamma3(i+1,:))+(90-h22(i,:))+0.26); % or j or whatelse nobody can know since you used 4 indices
else
Snna(i,:)=(w*sind(abs(Gamma3(i,:))))./(tand(h22(i,:)));
end
end
heir ancestors
el 27 de En. de 2017
Editada: heir ancestors
el 27 de En. de 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?
Categorías
Más información sobre Gamma Functions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!