繁体   English   中英

“索引超出矩阵维度”错误

[英]"Index exceeds matrix dimensions" error

我运行一个代码,“索引超出矩阵维度”显示为错误,但我不明白为什么。

这是代码:

function [ p ] = myIsort2(p)
%myIsort2 is based on myIsort but instead of sorting a row vector into 
%increasing order it sorts a structure array into decreasing order

global order

n=length(p);

for i=2:n

    x=p(1,i).exponent;
    y=p(1,i).coeff;
    j=i-1;

    while (j~=0) && order(x,p(1,j).exponent)==1

        %compares the order between 2 row vectors of the exponential field 
        %in order to sort them by making the smallest one come after the
        %largest one

        p(1,j+1).exponent=p(1,j).exponent;
        p(1,j+1).coeff=p(1,j).coeff;
        j=j-1;

    end

    p(1,j+1).exponent=x;
    p(1,j+1).coeff=y;

end

end

谢谢。

问题可能是访问p ,它是通过从p(1,1)p(1,n)索引访问的, n = length(p)

如果你得到一个index exceeds matrix dimensions错误,结论是p少于n列。 请注意, lengthp最大维度的大小。 因此,如果p行数多于列数,则会显示此错误。

一个例子:

  • 假设p<10x5 double>
  • n = length(p)返回n = 10
  • 但是, p(1,10)返回Error: index exceeds matrix dimensions因为p只有 5 列。

而不是length ,使用size获取所有维度的大小,或使用numel获取元素总数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM