Matlab code not running (no error found)

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 个评论

We cannot run your code because several variables are undefined. So how should we be able to help ?
Can you please specify which variables? Thanks!
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
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?
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.
"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.
it is a line balancing problem, what i'm trying to achieve is realted to resource allocation, code is a complex using e, l, x, st, st1, and h`, in order to identify the optimal solution. The NSmax variable defines the maximum number of elements that can be allocated to a resource, ns is the number of elements already allocated to a resource, and ns0 and ns1 define the the allocated. loop is through the resources at incrementing The t im` variables and the index of the corresponding element. The nsh variable is used to store the number of resources that can be allocated to the resource. The np array stores the number of resources that a to check if np the resource using and `` variables if enough resources allocate. The stpi and stfi variables are used to store the total number of resources allocated to the element and the number of resources allocated to the resource, respectively. The x variable is used to store the number of resources allocated to a particular element, and the st and st1 variables are used to store the total number of resources allocated to the resource and the total number of resources allocated to the element, respectively. Finally, the disp function is used to display the values of the different variables. This algorithm of allocating resources
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.

请先登录,再进行评论。

回答(1 个)

Suman
Suman 2023-2-13
编辑:Suman 2023-2-13
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

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

编辑:

2023-2-13

Community Treasure Hunt

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

Start Hunting!

Translated by