how can I write a while condition for an empty array?

I write a while loop as:
while set==[]
....
....
end
but when I run my code it only works until the while loop, not enters inside the loop and on the text of the while set==[] there is an error message as: operator '[' is seldom used in a scalar context.
What is the problem? I want my loop run until my set array has no element. If required I can send my code...
Thanks in advance... Regards...

2 个评论

Do NOT name any variable set, as this is the name of a very important inbuilt function set. Using the names of inbuilt functions and values is a very bad idea. You should always check if the name exists using which name
I write my variable's name in Turkish. But here I translate it in English as set. In fact I doesn't write in the code as set. But nevertheless thanks for your warning...

请先登录,再进行评论。

回答(1 个)

Use isempty:
while isempty(value)
...
end

4 个评论

I write as your answer but still it doesn't enter the loop. I wonder whether there is a mistake on my code?
clc, clear all;
task=11; % task number
pre= [0 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 1 0 0 0 0 0 0 0 0 0;
1 0 1 1 1 0 0 0 0 0 0;
1 1 0 0 0 1 0 0 0 0 0;
1 0 1 1 1 0 1 0 0 0 0;
1 1 0 0 0 1 0 1 0 0 0;
1 0 1 1 1 0 1 0 1 0 0]; % precedeccors matrix
succ=[0 1 1 1 1 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 0 1 0 0 0;
0 0 0 0 0 0 0 0 1 0 0;
0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 1;
0 0 0 0 0 0 0 0 0 0 1;
0 0 0 0 0 0 0 0 0 0 0]; %immediate succesor matrix
%step 1:
AA= find(all(pre==0,2)) %set of tasks that has no immediate task
string=[]; % emty string
%step 2:
while isempty(AA)
%step 3:
msize=numel(AA);
idx=randperm(msize);
F=AA(idx(1:1)) % randomly choosing a task
string=F %assign randmly chosen task to the string
%step 4:
m=intersect(AA,F)
AA(AA==m)=[] % delete the randmly choosen task from set
h=find(succ(m,:)==1) % immediate succeccor tasks of the deleted task
z=find(all(pre(h,:)==1)) % tasks should be assigned before the immediate succecor tasks
if ismember(z,string)
AA=h
else
AA=z
end
end
We don't know the rest of your code so we cannot say whether there are mistakes in it.
Because the first row is all zeros, the while loop will never begin:
pre= [0 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 0 0 0 0 0;
1 1 0 0 0 0 0 0 0 0 0;
1 0 1 1 1 0 0 0 0 0 0;
1 1 0 0 0 1 0 0 0 0 0;
1 0 1 1 1 0 1 0 0 0 0;
1 1 0 0 0 1 0 1 0 0 0;
1 0 1 1 1 0 1 0 1 0 0];
AA = find(all(pre==0,2))
when I run this it displays this in my command window:
AA =
1
so AA is not empty, and your while loop will never start. Perhaps you need to fix the algorithm that you are using.
Suggestion: instead of trying to fix broken code, if you actually tell us exactly what you are trying to achieve then we could advise some neat and efficient ways to achieve this.
Ok. I want to obtain a feasible sequence for the assembly line tasks that have a precedence relationship. My algorithm is as:
Step 1: form an initial available set of tasks having no predecessors, and create an empty string.
Step 2: terminate, if the available set is empty. Otherwise, go to Step 3.
Step 3: select a task from the available set at random, and append it to the string.
Step 4: update the available set by removing the selected task and by adding every immediate successor of the task if all the immediate predecessors of the successor are already in the string. Go to Step 2.
This is what I want to do. According to the algorithm I wrote this code. Without while loop it works true. But when I write thee loop it doesn't enter inside the loop.

请先登录,再进行评论。

类别

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

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by