Long and detailed AND,OR conditional IF statements on MATLAB - matlab

Could you please tell me what my mistake is on the following code:
if ((i>156-9 && i<156+9) && (j>406-9 && j<406+9)) || ((i>684-11 && i<684+11) && (j>274-11 && j<274+11)) || ((i>1066-15 && i<1066+15) && (j>67-15 && j<67+15)) || ((i>1559-15 && i<1559+15) && (j>867-15 && j<867+15)) || ((i>1082-18 && i<1082+18) && (j>740-18 && j<740+18))
plot(j, i, 'r+', 'MarkerSize', 7, 'LineWidth', 5);
end
I'd like to use an if statement which obeys to get in the expression if one of these conditions are fulfilled:
((i>156-9 && i<156+9) && (j>406-9 && j<406+9)) OR
((i>684-11 && i<684+11) && (j>274-11 && j<274+11)) OR
((i>1066-15 && i<1066+15) && (j>67-15 && j<67+15)) OR
((i>1559-15 && i<1559+15) && (j>867-15 && j<867+15)) OR
((i>1082-18 && i<1082+18) && (j>740-18 && j<740+18))
Thanks in advance...
Regards.

I have used your expression in the if as is and used this for this little piece of code.
clc; A=[];
for i=1:2000
for j=1:2000
if ((i>156-9 && i<156+9) && (j>406-9 && j<406+9)) || ((i>684-11 && i<684+11) && (j>274-11 && j<274+11)) || ((i>1066-15 && i<1066+15) && (j>67-15 && j<67+15)) || ((i>1559-15 && i<1559+15) && (j>867-15 && j<867+15)) || ((i>1082-18 && i<1082+18) && (j>740-18 && j<740+18))
A = [A, num2str(i),' ',num2str(j), char(10)];
end
end
end
display(A);
It works for me.

Related

What is the AND statement for HTCondor HPC?

It seems this HPC is hard to google docs. How does one write an AND statement for it? e.g. for OR one does:
requirements = (CUDADeviceName == "Quadro RTX 6000") || (CUDADeviceName == "NVIDIA A40")
would:
requirements = (CUDADeviceName != "Tesla K40m") /\ (CUDADeviceName == "NVIDIA A40")
work? What I want is NOT tesla k40m and NOT A40. Anything else is fine.
Gives error:
(metalearning_gpu) miranda9~/diversity-for-predictive-success-of-meta-learning $ condor_submit job.sub
Submitting job(s)ERROR: Parse error in expression:
Requirements = ((CUDADeviceName != "Tesla K40m") /\ (CUDADeviceName != "NVIDIA A40")) && (TARGET.Arch == "X86_64") && (TARGET.OpSys == "LINUX") && (TARGET.Disk >= RequestDisk) && (TARGET.Memory >= RequestMemory) && (TARGET.Cpus >= RequestCpus) && (TARGET.gpus >= Requestgpus) && ((TARGET.FileSystemDomain == MY.FileSystemDomain) || (TARGET.HasFileTransfer))
For an AND/conjuction this seems to work:
requirements = (CUDADeviceName != "Tesla K40m") && (CUDADeviceName != "NVIDIA A40")
Yes, you want && for conjunction.
For a complete tutorial of the classad expression language, see the manual section at https://htcondor.readthedocs.io/en/latest/classads/index.html
Or, there's a YouTube tutorial about the expression language at
https://www.youtube.com/watch?v=Y8aHj8q56ik

If statement in a Loop

I want my code to check if my 'e' components are in range of +,- 0.72 to +,- 0.73 to store the magnitude of Delta_theta and M in a vector for Plot. But I don't really know what is wrong in the last if statement. I just tried to store the amount of M that satisfies this condition in another matrix 'G' and plot it against delta theta, but it doesn't work either.
With kind regards.
I am sure the second and all other statements are correct, I just need help for the last one, thanks
Problem is Here
if e(Idx_1,Idx_2) >=0.072 && e(Idx_1,Idx_2) < 0.077 || e(Idx_1,Idx_2) <=-0.072 && e(Idx_1,Idx_2) > -0.077;
G=M;
end
clear all
clc
%% Constants
B=1.2;
H=0.35;
R=2.93;
b=1;
h=0.24;
d_c=0.06;
D_s=0.03;
d_s=0.4;
r_s=0.015;
N_s=2;
E_s=200e9;
f_y=500e6;
E_c=43.6e9;
v_c=0.24;
f_c=35.5e6;
F=1.9;
A_s=pi*r_s^2;
e=0.3*h;
%% Operator
Idx_1 = 0;
for delta_theta =-40*10^-3:0.05*10^-3:40*10^-3
alpha_e=0;
alpha_p=0;
beta_e=0;
beta_p=0;
r_b=0;
r_s=0;
r_e=0;
Idx_1 = Idx_1 + 1;
Idx_2 = 0;
delta_thetavalue(Idx_1)=delta_theta;
A=r_s-(f_y*d_s/(E_s*delta_theta));
for delta_l=-10*10^-2:0.05*10^-2:10*10^-2
Idx_2 = Idx_2 + 1;
delta_lvalue(Idx_2)=delta_l;
r_0=R-(delta_l/delta_theta);
r_1=R-((1/delta_theta)*(delta_l+(F*f_c*d_c)/E_c));
if delta_theta >=0
r_op=R+(h/2);
r_cl=R-(h/2);
if r_0 >= r_op && r_1<=r_cl
alpha_e=1;
r_b=r_op;
r_e=r_cl;
elseif r_0 >=r_op && r_cl<r_1 && r_1<r_op
alpha_e=1;
alpha_p=1;
r_b=r_op;
r_e=r_1;
elseif r_0 >=r_op && r_1>=r_op
alpha_p=1;
r_b=r_op;
r_e=r_op;
elseif r_s<r_0 && r_0<r_op && r_1<=r_cl
alpha_e=1;
r_b=r_0;
r_e=r_cl;
elseif r_s<r_0 && r_0<r_op && r_1>r_cl
alpha_e=1;
alpha_p=1;
r_b=r_0;
r_e=r_1;
elseif A<r_0 && r_cl<r_0 && r_0<=r_s && r_1<=r_cl
alpha_e=1;
beta_e=1;
r_b=r_0;
r_e=r_cl;
elseif A<r_0 && r_cl<r_0 && r_0<=r_s && r_1>r_cl
alpha_e=1;
alpha_p=1;
beta_e=1;
r_b=r_0;
r_e=r_1;
elseif A>=r_0 && r_0>r_cl && r_1<=r_cl
alpha_e=1;
beta_p=1;
r_b=r_0;
r_e=r_cl;
elseif A>=r_0 && r_0>r_cl && r_1>r_cl
alpha_e=1;
alpha_p=1;
beta_p=1;
r_b=r_0;
r_e=r_1;
elseif r_0 <= r_cl
r_b=r_cl;
r_e=r_cl;
end
else
r_op=R-(h/2);
r_cl=R+(h/2);
if r_0 <= r_op && r_1 >= r_cl
alpha_e=1;
r_b=r_op;
r_e=r_cl;
elseif r_0 <=r_op && r_1>r_op && r_cl>r_1
alpha_e=1;
alpha_p=1;
r_b=r_op;
r_e=r_1;
elseif r_0 <=r_op && r_1<=r_op
alpha_p=1;
r_b=r_op;
r_e=r_op;
elseif r_s>r_0 && r_0>r_op && r_1>=r_cl
alpha_e=1;
r_b=r_0;
r_e=r_cl;
elseif r_s>r_0 && r_0>r_op && r_1<r_cl
alpha_e=1;
alpha_p=1;
r_b=r_0;
r_e=r_1;
elseif A>r_0 && r_cl>r_0 && r_0>=r_s && r_1>=r_cl
alpha_e=1;
beta_e=1;
r_b=r_0;
r_e=r_cl;
elseif A>r_0 && r_cl>r_0 && r_0>=r_s && r_1<r_cl
alpha_e=1;
alpha_p=1;
beta_e=1;
r_b=r_0;
r_e=r_1;
elseif A<=r_0 && r_0<r_cl && r_1>r_cl
alpha_e=1;
beta_p=1;
r_b=r_0;
r_e=r_cl;
elseif A<=r_0 && r_0<r_cl && r_1<=r_cl
alpha_e=1;
alpha_p=1;
beta_p=1;
r_b=r_0;
r_e=r_1;
elseif r_0 >= r_cl
r_b=r_cl;
r_e=r_cl;
end
end
M(Idx_1,Idx_2)=alpha_e*(E_c*b/d_c)*((delta_l/2)*((r_b-R)^2-(r_e-R)^2)+(delta_theta/3)*((r_b-R)^3-(r_e-R)^3))+alpha_p*(-F*f_c*b*abs(r_cl-r_e)*(((r_cl+r_e)/2)-R))+beta_e*E_s*A_s*((delta_l/d_s)+(delta_theta/d_s)*(r_s-R))*(r_s-R)+beta_p*f_y*A_s*(r_s-R);
N(Idx_1,Idx_2)=alpha_e*(E_c*b/d_c)*(delta_l*(r_e-r_b)+(delta_theta/2)*((r_e-R)^2-(r_b-R)^2))+alpha_p*(-F*f_c*b*abs(r_cl-r_e))+beta_e*E_s*A_s*((delta_l/d_s)+(delta_theta/d_s)*(r_s-R))+beta_p*f_y*A_s;
e=M./N;
end
if e(Idx_1,Idx_2) >=0.072 && e(Idx_1,Idx_2) < 0.077 || e(Idx_1,Idx_2) <=-0.072 && e(Idx_1,Idx_2) > -0.077;
G=M;
end
end
plot(delta_thetavalue,G)
We cannot check how your function e works but have you tried wrapping your conditions in parenthesis so it understand that it need to check them in pair?
if (e(Idx_1,Idx_2) >=0.072 && e(Idx_1,Idx_2) < 0.077) || (e(Idx_1,Idx_2) <=-0.072 && e(Idx_1,Idx_2) > -0.077);
also it could be easier to debug if you evaluate it once
evaluation = e(Idx_1,Idx_2); // you can print or anything to check the value before the if expression
if (evaluation >=0.072 && evaluation < 0.077) || (evaluation <=-0.072 && evaluation > -0.077);
Since you specifically ask for help for the last if statement, I can tell you there is nothing wrong with it. It appears your "e" function might have some bugs. Here is a dummy code snippet for you to play with the eVal which is a placeholder for your e(arg1, arg2) function. You should narrow this down and make sure where the error is by using the built in debugger.
clc;
eVal = 0.076;
G = zeros(3);
M = eye(3);
if eVal >= 0.072 && eVal < 0.077 || eVal <= -0.072 && eVal > -0.077;
G = M;
end
disp(G); % Should return eye(3) if the statement is true
I solved this problem like this :
t=0;
for Idx_1=1:1601 ;
for Idx_2=1:401;
if (e(Idx_1,Idx_2) >=0.072 && e(Idx_1,Idx_2) < 0.073) || (e(Idx_1,Idx_2) <=-0.072 && e(Idx_1,Idx_2) > -0.073);
t=t+1;
dt(t)=delta_thetavalue(Idx_1);
mf(t)=M(Idx_1,Idx_2);
end
end
end
plot(dt,mf)

LINQ slow even before getting data?

I have following query:
var sw = new Stopwatch();
sw.Start();
var model = new StatusModel();
IQueryable<StatusView> statusViewResult = db.StatusViewList;
statusViewResult = statusViewResult.Where(p =>
(query.ClustersSelected.Count == 0 || query.ClustersSelected.Contains(p.Cluster)) &&
(query.ParkNamesSelected.Count == 0 || query.ParkNamesSelected.Contains(p.ParkName)) &&
(query.TurbineNumbersSelected.Count == 0 || query.TurbineNumbersSelected.Contains(p.TurbineNumber)) &&
(query.ModelNamesSelected.Count == 0 || query.ModelNamesSelected.Contains(p.ModelName)) &&
(query.AlarmLevelSelected.Count == 0 || query.AlarmLevelSelected.Contains(p.AlarmLevel)) &&
(query.ParametersSelected.Count == 0 || query.ParametersSelected.Contains(p.Parameter)) &&
(query.TurbineTypesSelected.Count == 0 || query.TurbineTypesSelected.Contains(p.TurbineType)));
sw.Stop();
var d = sw.Elapsed.TotalMilliseconds;
So I am not doing any ToList() so I shouldn't be querying the database at this moment. However I takes around 2 seconds just for building this query - am I missing something here or is this expected?

Formatting of new line indentation of if conditions in Eclipse CDT

In Eclipse (4.4) CDT (8.5) on windows (7) the indentation of new lines within if and else if conditions does not make sense to me. Where in eclipse is this set?
I have tried looking in Window > Preferences > C/C++ > Code Style > Formatter but could not see any relevant setting.
An example where I hit the return key as I type the conditions or select the lines and press Ctrl+I ("Fix Indentation"). Note that the first four lines all have the same indentation despite differing levels of nesting. The following fifth line indents two more levels than the preceding line, as does the seventh line both with no apparent relation to the level of nesting.
else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
&& (rp != SMB_Protocol_Process_Call)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Write_Word) && (qcn >=2)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Block_Write)
&& (qcn >= (CMD_Number_Bytes_max + 1)))) {
e = EXIT_FAILURE;
}
I would prefer something along the lines of the following:
else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
&& (rp != SMB_Protocol_Process_Call)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Write_Word) && (qcn >=2)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Block_Write)
&& (qcn >= (CMD_Number_Bytes_max + 1)))) {
e = EXIT_FAILURE;
}
Is it possible to change this?
Also if someone could comment with an explanation of the current indentation/nesting formatting it would be appreciated!
EDIT: Realised this may be similar to Eclipse JDT Code Formatter: Indent parameter in IF-Statement
Is it possible to change this?
Yes. With the default indentation strategy (K&R built-in) I get the same as you with Ctrl+I, but if I do Ctrl+Shift+F (format code) I get the following.
} else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
&& (rp != SMB_Protocol_Process_Call)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Write_Word) && (qcn >= 2)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Block_Write)
&& (qcn >= (CMD_Number_Bytes_max + 1)))) {
If you want to get column alignment, rather than simple indentation on continued lines, create your own formatting profile and change the following:
Indentation tab -> Tab policy to either Spaces only or Mixed
With the default of Tabs only the indents will be to the next tab stop
Line Wrapping tab -> Expressions/Binary Expressions set Indentation policy to Indent on column
This is what you end up with when you format (but not when you simply correct indentation):
} else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
&& (rp != SMB_Protocol_Process_Call)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Write_Word) && (qcn >= 2)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Block_Write) && (qcn
>= (CMD_Number_Bytes_max + 1)))) {
Here are screenshots showing the settings:
and

Last elseif statement does not execute

My last elseif statement does not execute even if the conditions are met:
Currency_Exchanage != 'Select...' and all other variables (ETF_Exchanage, Index_Exchanage and Stock_Exchanage) = 'Select...'
Here is the section of code that I am concerned about:
if (strcmp(ETF_Exchanage,'Select...') == 1) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1)
if db == 1 && uni == 1
tickers = gnr_bloomberg; % Analsise Bloomberg natural resources
nrm=1;
elseif db == 1 && uni == 2
tickers = all_bloomberg; % Analsise Bloomberg all
nrm=1;
elseif db == 2 && uni == 1
tickers = gnr_yahoo; % Analsise Yahoo natural resources
nrm=1;
elseif db == 2 && uni == 2
tickers = all_yahoo; % Analsise Yahoo all
nrm=1;
end
else
%Yahoo inputs
if (strcmp(ETF_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from ETF
tickers = ETF_Yahoo(:,1);
Exchanges = ETF_Yahoo(:,2);
Exchange = ETF_Exchanage;
db=2; %Yahoo Selection
elseif (strcmp(Index_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(ETF_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from Index
tickers = Index_Yahoo(:,1);
Exchanges = Index_Yahoo(:,2);
Exchange = Index_Exchanage;
db=2;
elseif (strcmp(Stock_Exchanage,'Select...') == 0) && (strcmp(ETF_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(Currency_Exchanage,'Select...') == 1); %Choose exhanges from Stock
tickers = Stock_Yahoo(:,1);
Exchanges = Stock_Yahoo(:,2);
Exchange = Stock_Exchanage;
db=2;
elseif (strcmp(Currency_Exchanage,'Select...') == 0) && (strcmp(Stock_Exchanage,'Select...') == 1) && (strcmp(Index_Exchanage,'Select...') == 1)...
(strcmp(ETF_Exchanage,'Select...') == 1); %Choose exhanges from Currency
tickers = Currency_Yahoo(:,1);
Exchanges = Currency_Yahoo(:,2);
Exchange = Currency_Exchanage;
db=2;
else
msg = 'Error occurred.\Only one Yahoo input menue must be used!';
error(msg)
end
end
Any Help would be much appropriated, I can't see where I'm going wrong here. I am using Matlab 2013a.
Put a breakpoint at the elseif statement in question and then check in the command window what your condition evaluates to.
If it does not evaluate like expected, check what the individual terms evaluate to.
It is important to actually test what the conditions evaluate to in matlab, rather than only visually comparing the string values.
Usually by that point you should get a rough idea what is wrong.
However in your case we can't do these steps for you because something is off. Your code condensed to the more reasonable minimal example
if 1 && 1 && 1...
1;
disp('I was here')
end
does not even execute in R2014a since the interpreter complains about '...' being an unexpected matlab expression.