IF CONDITION NOT WORKING

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

Answers (1)

Niels
Niels on 23 Jan 2017
Edited: Niels on 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 Comments

Hello Niels , Thank you very much for your answer ;
But how to put the condition element by element , I tried the loop for , but still the same problem , like I want to make the condition only for the element, element by element ...
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
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.
Hello, thank you very much guys for your answers ; it's a very good observation from Walter ; I took my time to try every possibility, but the results still not here ;
I will explain my code : [e1,o1]=size(Gamma3); [r1,r2]=size(h22); Snna=zeros(e1,o1) ; % Gammaa = [6 7] , h22=[6 7]
for ja1=1:e1-1
for ha1=2:e1
for xa1=1:r1-1
for xa2=1:r2
for ia1=1:o1
if (Gamma3(ja1,ha1)<0) % here I want detect every negative element%
Snna(ja1,:)=(w*sind(abs(Gamma3(ja1,:))))./(tand(h22(xa1,:)))
here I want to calculate Snna but line by line --> I mean this :
A11 A21 A31 A41 ... % I want to calculate : A11, A12, A13...after
A12 A22 A32 A42 ... A21 A22 A23 ..after A31,A32,A33.....%
A13 A23 A33 A43 ...
. . . . ...
else
Snna(ja1,:)=w*sind(abs(Gamma3(ja1,:))).*tand((2*abs(Gamma3(ha1,:)))+(90- h22(xa1,:)+0.26))
%%here the same thing, % I want to calculate : A11, A12, A13...after
A21 A22 A23 ..after A31,A32,A33.....%
% the problem is the code is acting like ,if the first element is negative , it's applies the negative condition for all the other elements of the other lines even if they're not negative ! I tried also with if any(Gamma<0) as suggested by Star, Also I tried Snna(ja1,ia1)( iteration lines,columns, but in this case , I'm not calculating line by line ..I want to calculate line by line with stopping the colummns and after go to the other column ..etc )%
end
end
end
end
end
end
end
Thank you very much for helping ..
Niels
Niels on 26 Jan 2017
Edited: Niels on 27 Jan 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
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 on 27 Jan 2017
Edited: Niels on 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?

Sign in to comment.

Asked:

on 23 Jan 2017

Edited:

on 27 Jan 2017

Community Treasure Hunt

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

Start Hunting!