簡體   English   中英

Matlab:索引超出矩陣尺寸

[英]Matlab: Index exceeds matrix dimensions

我正在嘗試運行以下代碼:

classdef HelloWorld
    properties
        var;
        array;
    end

    methods
        function h = HelloWorld()
            h.var = 30;
            setArray(h);
            disp(h.array(10));
        end

        function setArray(h)
            for i=1:h.var
                h.array(i) = i*2;
            end
        end
    end
end

但是,出現以下錯誤:

Index exceeds matrix dimensions.

Error in HelloWorld (line 14)
        disp(h.array(10));

因為您正在訪問一個空數組。

您需要的是HelloWorld的新副本,該副本具有已初始化的數組。

classdef HelloWorld
    properties
        var;
        array;
    end

    methods
        function h = HelloWorld()
            h.var = 30;
            h=setArray(h);
            disp(h.array(10));
        end

        function h=setArray(h)
            for i=1:h.var
                h.array(i) = i*2;
            end
        end
    end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM