Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab:i=2,如果B(i)>;B(i+;1)。。。下标索引错误,必须为正整数_Matlab_Indexing_Subscript - Fatal编程技术网

Matlab:i=2,如果B(i)>;B(i+;1)。。。下标索引错误,必须为正整数

Matlab:i=2,如果B(i)>;B(i+;1)。。。下标索引错误,必须为正整数,matlab,indexing,subscript,Matlab,Indexing,Subscript,我正在为类编写排序算法,第14行出现了这个错误,下标索引必须是实正整数或逻辑数。我用不同的思路寻找答案,但答案似乎令人困惑,与我的问题并不相关。我理解错误的含义,但我不明白代码失败的原因。i=2是一个正整数,没有非整数或负整数的除法或乘法,据我所知,下标索引的位置没有零。我不明白。提前感谢您的帮助 function bubblesort(A) %bubble sorting algo B=A; c=numel(B); %count the number of elements in a, sto

我正在为类编写排序算法,第14行出现了这个错误,下标索引必须是实正整数或逻辑数。我用不同的思路寻找答案,但答案似乎令人困惑,与我的问题并不相关。我理解错误的含义,但我不明白代码失败的原因。i=2是一个正整数,没有非整数或负整数的除法或乘法,据我所知,下标索引的位置没有零。我不明白。提前感谢您的帮助

function bubblesort(A)
%bubble sorting algo
B=A;
c=numel(B);
%count the number of elements in a, store it as c
if B(1)>B(2)
    left=B(1);
    right=B(2);
    B(1)=right;
    B(2)=left;
end
i=2;
while i+1<=c
    **if B(i)>B(i+1)**
        left=B(i);
        right=B(i+1);
        B(i)=right;
        B(i+1)=left;
        i=i-1;
    else
        i=i+1;
    end
end
B
end
函数bubblesort(A)
%气泡排序算法
B=A;
c=努美尔(B);
%计算a中的元素数,将其存储为c
如果B(1)>B(2)
左=B(1);
右=B(2);
B(1)=对;
B(2)=左;
终止
i=2;
而i+1B(i+1)**
左=B(i);
右=B(i+1);
B(i)=对;
B(i+1)=左;
i=i-1;
其他的
i=i+1;
终止
终止
B
终止
问题在于(除非A的第一个元素已经是最小的),i=i-1行将使最小元素的i=0,代码将失败。我认为如果I>1,这个函数可以被修正

function bubblesort(A)
%bubble sorting algo
B = A;
c = numel(B);
%count the number of elements in a, store it as c  

i = 1;
while i+1 <= c
    if B(i) > B(i+1)
        left = B(i);
        right = B(i+1);
        B(i) = right;
        B(i+1) = left;
        if i > 1
            i = i-1;
        end
    else
        i = i+1;
    end
end
B
end
函数bubblesort(A)
%气泡排序算法
B=A;
c=努美尔(B);
%计算a中的元素数,将其存储为c
i=1;
而i+1B(i+1)
左=B(i);
右=B(i+1);
B(i)=对;
B(i+1)=左;
如果i>1
i=i-1;
终止
其他的
i=i+1;
终止
终止
B
终止

我正在运行你的函数,没有给我任何错误。你能提供一个数组的例子让它遇到那个错误吗?就是这样!非常感谢你!