Matlab code not running (no error found)
Show older comments
I have written the following code that involves multiple loops that returnes vlaues in an array. there is no sytax error, however, results are not obained, I do not know what the problem is because it detects nothing wrong and gives no error message. I know that I can just define one more varible or function to complete this task.
Here is the code:
NSmax=max(NS);
M=0;
E = zeros(NSmax,M);
L = zeros(NSmax,M);
X = zeros(NSmax,M,M,NSmax);
ST = zeros(NSmax,M);
ST1 = zeros(NSmax,M);
h=1;
NS=1;
NS0=0;
NS1=0;
NSmax=0;
while h<=M
m = h;
t = T(m:m,:);
t = t(1:NS(m));
Im = I(m:m, :);
Im = Im(1:NS(m))';
NSh = floor(sum(t)/C(m))+1;
NS0=NS1+NS0;
NS1 = NS(m);
if m ~= 1
np = NP((NS0+1):(NS0+NS1),:);
else
np = NP(1:NS1);
end
a1 = numel(np);
while a1 > 0
a2 = sum(np == 0);
while a2 > 0
b=find(np == 0);
c=b(1,1);
U=Im(b);
i=U(1,1);
z=find(Im == i);
np(c)= [];
Im(z)= [];
stpi=0;
a3=NS0+i;
if NP(a3) > 0
for j=1:NP(a3)
s=P(a3,j);
stpi=stpi+T(h,s);
end
else
stpi=0;
end
E(i,h)=floor((T(h,i)+stpi)/C(h))+1;
%disp(sprintf('E(%d,%d) = %0.0g', i, h, E(i,h)));
stfi=0;
a4=NS0+i;
if NF(a4) > 0
for j=1:NF(a4)
s=F(a4,j);
stfi=stfi+T(h,s);
end
else
stfi = 0;
end
L(i,h) = NSh-floor((T(h,i)+stfi)/C(h));
%disp(sprintf('L(%d,%d) = %0.0f', i, h, L(i,h)));
k=E(i,h);
a5 = X(i,m,h,k);
while a5 ~= 1
ST(k,h) = T(h,i)*X(i,m,h,k)+ST(k,h);
ST1(k,h) = ST(k,h)+T(h,i);
if k <= L(i,h)
if ST1(k,h) <= C(h)
ST(k,h)=ST1(k,h);
X(i,m,h,k)=1;
else
k=k+1;
end
else
NSh = NSh+1;
L(i,h) = k;
if ST1(k,h) <= C(h)
X(i,m,h,k) = 1;
ST(k,h)=ST1(k,h);
L(i,h) = k;
end
end
a5 = X(i,m,h,k);
if a5 == 1
disp(fprintf('E(%d,%d) = %0.0g', i, h, E(i,h)));
disp(fprintf('L(%d,%d) = %0.0f', i, h, L(i,h)));
disp(fprintf('X(%d,%d,%d,%d) = %0.0f', i, m, h, k, X(i,m,h,k)));
disp(fprintf('ST(%d,%d) = %0.0f', k, h, ST(k,h)));
end
end
end
end
end
8 Comments
Torsten
on 10 Feb 2023
We cannot run your code because several variables are undefined. So how should we be able to help ?
roaa zamkah
on 10 Feb 2023
Dyuman Joshi
on 10 Feb 2023
NS, NP, etc.
However, your code doesn't run because the while loop is not executed as M is smaller than h.
M=0;
h=1;
while h<=M
end
roaa zamkah
on 10 Feb 2023
Jan
on 10 Feb 2023
Some notes:
- Simplify your code:
t = T(m, :); % instead of t = T(m:m,:);
- Use a proper indentation: Ctrl-a Ctrl-i
fprintf('E(%d,%d) = %0.0g\n', i, h, E(i,h)); % No disp() required
- Without useful comments, such a code is useless. No reader will understand its purpose, including yourself in 3 month. Debugging and maintaining is too hard.
- Use the debugger to step through the code line by line. Then you will see, what is happening inside.
Dyuman Joshi
on 10 Feb 2023
Edited: Dyuman Joshi
on 10 Feb 2023
"I have written the following code ..."
The code you have written above is incomplete, missing definition of many variables as Torsten mentioned.
"i dont know if this will help, but the codes reads a certain data from an excel file. also, what should (T) be defined as?"
We don't know what you are trying to do nor we know what the data is. We can't say what T should be defined as.
On what basis have you written the code? Are you trying to solve a certain problem? Please specify what you are trying to achieve.
Also, providing a sample input and a respective output will help you solve the problem sooner.
Without more information, any help will be limited to a few suggestions only.
roaa zamkah
on 10 Feb 2023
Torsten
on 10 Feb 2023
If you formulate your problem as an optimization problem in a mathematical way and include it here, we might be able to offer a code from the MATLAB software to solve it.
But nobody in this forum will dive into the above code to decipher it, I guess.
Answers (1)
Hi Roaa,
It seems there are a few issues with the code that you have provided:
- In Line-1, the variable "NS" is used, but is declared later in the code. So, either the declaration statement has to be before Line-1 OR maybe it's already there and you haven't provided the full code.
- In Line-14, the while loop condition "h<=M" will never be satisfied because "h=1" and "M=0" are declared before and h>M.
You can debug the code accordingly as per your requirements.
You may set "Breakpoints" to debug the code and use "Step" functionalility in MATLAB to navigate through the codeflow.
I hope this information is helpful to you.
Cheers,
Suman
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!