how to make if statement repeat only once ?
39 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
hello I am currently doing a project with matlab and simulink.
In my project I am taking current samples from matlab function and making is statment for a switch .
the problem is that my statment is working all the time , meaning that for exmaple my switch is on "1" and it is switching to "0" I want it to stay like this and not creak the statement again how is it possible ?
function [final_resolt]=relay_condition(u)
format long;
i_diff_1=u(1)-u(4);
i_diff_2=u(2)-u(5);
i_diff_3=u(3)-u(6);
ind_1=u(1)*u(4);
ind_2=u(2)*u(5);
ind_3=u(3)*u(6);
I_diff_1=0;
I_diff_2=0;
I_diff_3=0;
IND_1=1;
IND_2=1;
IND_3=1;
m=20;
I_min=1000;
for i=1:1:m
I_diff_1=(I_diff_1+i_diff_1^2);
I_diff_2=(I_diff_2+i_diff_2^2);
I_diff_3=(I_diff_3+i_diff_3^2);
IND_1=(IND_1*ind_1)/1;
IND_2=(IND_2*ind_2)/1;
IND_3=(IND_3*ind_3)/1;
end
I_diff_1=(I_diff_1/m)^0.5;
I_diff_2=(I_diff_2/m)^0.5;
I_diff_3=(I_diff_3/m)^0.5;
IND_1=IND_1/m;
IND_2=IND_2/m;
IND_3=IND_3/m;
if (I_diff_3>I_min )
final_resolt=0;
else
final_resolt=1;
end
end
3 comentarios
Guillaume
el 22 de Mayo de 2018
Since the if statement is outside of any loop, it will execute only once. Hence it's not clear what the problem is.
Respuestas (1)
Aakash Deep
el 22 de Mayo de 2018
Hey,
From your question, I understand that you have a if condition inside a loop which you need to execute only once for a particular condition. In order to do this you can use a flag variable. Let's see this with an example, let's suppose you have an array with values 0 and 1. You have to subtract value x only on the first zero in the array.
arr = [1,1,1,0,0,1,0,0];
flag = 0;
for i = 1:len(arr)
if( flag == 0 && arr(i) == 0 )
arr(i)= arr(i) – x;
flag = 1; % important part
end
% loop_continue
end
Hope this helps
Thanks,
Aakash Deep
1 comentario
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!