scilab 中 if-else 条件的问题

problem in the if-else condition in scilab

我在 SCILAB 中编写了一个 SSTF 磁盘调度算法代码,在 运行 期间,在正确显示前 3 个值 (41,34,11) 之后,它始终显示为 -1。有人可以告诉我代码中的问题是什么吗? 我也尝试给出不同的 if 条件,但它也没有给出正确的输出。

clear
clf;
seek = 0
x = 8
l = list(176,60,79,92,114,11,34,41) 
head = 50
mi = 1
for j = 1:x  
    for i=1:x
        if (l(i)~=(-1))then
            if( abs(head - l(i)) < abs(head -l(mi))) then
                mi = i
            end    
        end
    end    
    seek = seek + abs(head - l(mi))
    head = l(mi)
    h(j) = head
    se(j) = seek
    mprintf('Head is at %i and seek is %i\n',head,seek)
    l(mi) = -1
end

plot(h,se)
scatter(h,se)

你的问题是,在前 3 次迭代之后,head=11 比 60 更接近 -1。用 %nan(不是数字)替换 -1,结果这个值不会从未被视为最接近当前头部。此外,通过使用向量而不是列表,您可以使用 max

删除内部循环
clear
l = [176,60,79,92,114,11,34,41];
head = 50;
seek = 0;
for j = 1:8
    [step,im] = min(abs(head - l));
    seek = seek + step;
    head = l(im);
    mprintf('Head is at %i and seek is %i\n',head,seek);
    l(im) = %nan;
    h(j) = head
    se(j) = seek
end

clf
handle = plot(h,se)
scatter(h,se)

您得到以下输出:

Head is at 41 and seek is 9
Head is at 34 and seek is 16
Head is at 11 and seek is 39
Head is at 60 and seek is 88
Head is at 79 and seek is 107
Head is at 92 and seek is 120
Head is at 114 and seek is 142
Head is at 176 and seek is 204