if statement is not working

18 visualizaciones (últimos 30 días)
Lama Hamadeh
Lama Hamadeh el 8 de Dic. de 2021
Comentada: Abolfazl Chaman Motlagh el 9 de Dic. de 2021
Hi all,
I have the follwoing code that runs fine but no output of the if statement for some reason:
theta = [-90.0000 -63.4746 -52.1364 -43.1736 -35.3765 -28.2737 -21.6183 ...
-15.2575 -9.0847 -3.0170 3.0170 9.0847 15.2575 21.6183 28.2737 ...
35.3765 43.1736 52.1364 63.4746 90.0000];
x0 = 0.1250;
y0 = 0;
if theta >= 41.19 & theta <= 90
inter_r = -(1/sin(theta))*[-sin(theta) cos(theta)*(x0-1)-y0.*sin(theta)];
end
An error of (Unrecognized function or variable 'inter_r') occurs.
Any help would be appreicted.
Thanks.

Respuesta aceptada

Jon
Jon el 8 de Dic. de 2021
Editada: Jon el 8 de Dic. de 2021
The problem is that your if statement will only be true if all of the elements of theta satisfy the condition.
If you only want to assign inte_r for elements where the condition is met you could do something like this:
theta = [-90.0000 -63.4746 -52.1364 -43.1736 -35.3765 -28.2737 -21.6183 ...
-15.2575 -9.0847 -3.0170 3.0170 9.0847 15.2575 21.6183 28.2737 ...
35.3765 43.1736 52.1364 63.4746 90.0000];
x0 = 0.1250;
y0 = 0;
% assign logical vector which is true for elements that are in range
inRange = theta >= 41.19 & theta <= 90
% select elements of theta that are in range
thetaInRange = theta(inRange)
% assign function values just for elements that are in range
inter_r = -(1.0 ./sin(thetaInRange)).*...
  2 comentarios
Jon
Jon el 8 de Dic. de 2021
You also seem to have another problem in your function if theta is a vector. First of all you aren't consistent with your use of element by element multiplication and division. You use it in the final multiplication, but if theta is a vector then you would also need it for
-(1/sin(theta))
and also the next multiply.
You would also have a problem, even with element by element multiplication of
-(1./sin(theta)).*[-sin(theta) cos(theta)*(x0-1)-y0.*sin(theta)]
because 1 ./ sin(theta) has as many element as theta, but you multiply it by a new vector, defined by your square brackets that has twice as many elements.
Lama Hamadeh
Lama Hamadeh el 9 de Dic. de 2021
Editada: Lama Hamadeh el 9 de Dic. de 2021
Thanks Jon. That did solve the problem.

Iniciar sesión para comentar.

Más respuestas (1)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh el 8 de Dic. de 2021
the condition theta >= 41.19 & theta <= 90 is a logical vector with 20 values. if you want to calculate such a formula for those theta that satisfy the condition. you can use for loop or vectorize code.
theta = [-90.0000 -63.4746 -52.1364 -43.1736 -35.3765 -28.2737 -21.6183 ...
-15.2575 -9.0847 -3.0170 3.0170 9.0847 15.2575 21.6183 28.2737 ...
35.3765 43.1736 52.1364 63.4746 90.0000];
x0 = 0.1250;
y0 = 0;
for i=1:numel(theta)
if theta(i)>= 41.19 & theta(i)<= 90
inter_r(i,1:2) = -(1/sin(theta(i)))*[-sin(theta(i)) cos(theta(i))*(x0-1)-y0.*sin(theta(i))];
else
inter_r(i,1:2) = nan; % for example
end
end
or vectorize
Condition = theta >= 41.19 & theta <= 90;
inter_r(Condition,1:2)=repmat(-(1./sin(theta(Condition)))',[1 2]).* ...
[-sin(theta(Condition))' (cos(theta(Condition))*(x0-1)-y0.*sin(theta(Condition)))'];
inter_r(~Condition,:)=nan;
  2 comentarios
Lama Hamadeh
Lama Hamadeh el 9 de Dic. de 2021
Thanks for the reply. I tried the for loop method (which I find it very clear to use) but I am getting wrong output, I'm afraid.
Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh el 9 de Dic. de 2021
i see you accept another answer. it's good to hear it solves your problem. in case you still want to tell me what exactly is wrong in this answer, i can gladly help.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by