简体   繁体   English

Matlab:索引超出矩阵尺寸

[英]Matlab: Index exceeds matrix dimensions

I am trying to run the following code: 我正在尝试运行以下代码:

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

However, I get the following error: 但是,出现以下错误:

Index exceeds matrix dimensions.

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

Because you're accessing an empty array. 因为您正在访问一个空数组。

What you need is a new copy of HelloWorld which have an initialized array. 您需要的是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.

相关问题 分配给结构数组时在Matlab中“索引超过矩阵维数” - “Index exceeds matrix dimensions” in Matlab when assigning to a struct array Matlab 编码器中的错误:索引超出数组尺寸 - Error in Matlab Coder: Index exceeds array dimensions 避免“索引超出矩阵尺寸”的功能 - Function to avoid 'Index exceeds matrix dimensions' MATLAB min(array)给出的索引超出了数组的维数 - MATLAB min(array) gives index exceeds array dimensions 创建带状对角矩阵时,spdiags出现“索引超出矩阵尺寸”错误? - “Index exceeds matrix dimensions” error with spdiags when creating band diagonal matrix? 索引超过了 MATLAB 中的数组元素数 (9) - Index exceeds the number of array elements (9) in MATLAB MATLAB - position 2 中的索引超出数组边界(不得超过 1) - MATLAB - Index in position 2 exceeds array bounds (must not exceed 1) 如何修复此错误 position 2 中的索引超出 MATLAB 中的数组边界? - How to fix this error Index in position 2 exceeds array bounds in MATLAB? 应用于值数组时,索引超出矩阵维 - Index exceeds matrix dimension when applied to an array of values Matlab中的函数movmean是否创建了矩阵的所有维度的平均值? - Does the function movmean in Matlab create an average of all dimensions of the matrix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM