簡體   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