# My elseif condition is never checked. What to do?

2 views (last 30 days)
Laurian Hurduza on 26 Oct 2020
Commented: Image Analyst on 27 Oct 2020
Hello,
I want to ask a very noob question: My elseif condition is never checked. What to do?
So, let me explain.
I got this piece of code:
A = [0.4 1.2 0.5 0.7 0.2 0.6];
S = [2.0 0.7 0.2 1.1 3.7 0.6];
in(1) = 0.4;
out(1) = in(1)+S(1);
list = 0;
%calculation of ins vector
for i = 2:6
in(i) = in(i-1)+A(i);
%calculation of outs vector
for j = 2:6
out(i) = max(out(i-1),out(i))+S(i);
end
end
So, this program simulates the inputs and outputs in/from a server
Inputs(in) and outputs(out) are already calculated in above for
The idea is simple: if an input is done in server, list will increment by 1, if an output is done from server it will decrement by 1
A way to do this is to compare if, on time axe, we have inputs in server or outputs from it and to increment and decrement accordingly
Here everything I think looks clear for me, I know how to make those ifs
But the big challenge for me is to see the evolution of the list on a plot
So, I wrote this piece of code
i = 1; %ins
o = 1; %outs
n = 1; %every for iteration
for r = 0:0.1:12
if r == in(i)
list = list + 1;
i = i + 1;
elseif r == out(o)
list = list - 1;
o = o + 1;
end
list1(n)=list;
n = n + 1;
end
plot(l1,'-r')
ylabel('list')
xlabel('time')
grid;
My plot looks like this: Which is I intended, but it looks like the graph will never decrement, it will not go in elseif part.
What can I do in order to make my plot to go down ( to check that elseif as well ) in order to make it work on my plot?
List stores how many clients are not in server ( because one client is in there )
Time stores how much time did server had to take for each client
If I am unclear in what I am asking for, you can ask me to clear some things up.
Thank you for any kind of help.

Image Analyst on 26 Oct 2020
To compare floating point numbers, you need to use ismembertol(), not ==.
Like
if ismembertol(r, in(i), .0001)

Laurian Hurduza on 26 Oct 2020
I modified the code above, and I have something that looks like this:
i = 1; %ins
o = 1; %outs
n = 1; %every for iteration
for r = 0:0.1:12
if ismembertol(r, in(i), .0001)
list = list + 1;
i = i + 1;
elseif ismembertol(r, out(o), .0001)
list = list - 1;
o = o + 1;
end
list1(n)=list;
n = n + 1;
end
plot(l1,'-r')
ylabel('list')
xlabel('time')
grid;
But I recieve the following error:
Index exceeds the number of array elements (6).
Error in ex1 (line 28)
if ismembertol(r, in(i), .0001)
I just changed my conditions from
if r == in(i)
in
if ismembertol(r, in(i), .0001)
My array elements are 6, but the weird thing is that the code worked with wrong condition, but with the correct one, it doesn't..
What to do in this case?
Image Analyst on 27 Oct 2020
Please give us a script that runs, or at least plots. When I run your code, I get a error and there is no plot:
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
A = [0.4 1.2 0.5 0.7 0.2 0.6];
S = [2.0 0.7 0.2 1.1 3.7 0.6];
in(1) = 0.4;
out(1) = in(1)+S(1);
list = 0;
%calculation of ins vector
for i = 2:6
in(i) = in(i-1)+A(i);
%calculation of outs vector
for j = 2:6
out(i) = max(out(i-1),out(i))+S(i);
end
end
i = 1; %ins
o = 1; %outs
n = 1; %every for iteration
for r = 0:0.1:12
if r == in(i)
list = list + 1;
i = i + 1;
elseif r == out(o)
list = list - 1;
o = o + 1;
end
list1(n)=list;
n = n + 1;
end
plot(l1,'-r')
ylabel('list')
xlabel('time')
grid;
fprintf('\nDone running %s.m ...\n', mfilename);

R2020b

### Community Treasure Hunt

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

Start Hunting!