【问题标题】:Matlab subscript indices errorMatlab下标索引错误
【发布时间】:2016-08-26 01:27:36
【问题描述】:

请帮帮我。每当我将 0 值放在“数据”上时,我总是得到一个下标索引必须是真正的正整数或逻辑错误我怎样才能摆脱它,我需要在那个上放一个零。每当电压为零时(1,0)= 1。但我无法通过。

Voltage = [0 1 1 3 4 1; 1 0 5 4 5 3; 6 4 0 4 5 7; 9 3 4 0 6 4; 7 8 5 6 0 7; 4 5 6 7 3 0];
data =[0 2 3 4; 5 6 7 8; 2 3 4 5; 4 5 6 7; 3 4 5 6; 1 3 5 7; 1 2 3 4; 3 4 5 6];
Vm = data(:,1);
Vn = data(:,2);
R = data(:,3);``
X1 = data(:,4);
sz=max(Vn)
y=1:sz
for Vm=data(:,1)
    if Vm==0
       Voltage(y,Vm)=1
       Voltage(y,Vm)=logical(Voltage(y,Vm));
       Current = Voltage(y,Vm)-Voltage(y,Vn);
    else Vm >= 1
       Current = Voltage(y,Vm)-Voltage(y,Vn);
    end
end

【问题讨论】:

    标签: matlab subscript


    【解决方案1】:

    您正在尝试使用yVoltage 矩阵中的else 语句中引用一个值,但y 不是整数,而是一个数组(或一维矩阵)。如果您显示y,您将看到它是1 2 3 4 5 6。有几段违规代码,其中之一是:

    else Vm >= 1
        disp(y) # `y` is not an integer and therefore not a valid index.
        Current = Voltage(y,Vm)-Voltage(y,Vn);
    

    要修复它,请决定 y 应该是静态的还是在循环中更改。

    如果您需要进一步解释,请告诉我。

    【讨论】:

    • 您好,谢谢您的帮助,我需要进一步的解释,因为我希望我的“y”在循环中改变。你能解释一下我能用它做什么吗?
    • @well 你希望y 如何改变?例如,是否要在每次满足 else 条件时使用 y 矩阵中的下一个值?
    • 是的,想要这样。
    • @well y 矩阵的长度为 6,但您的 for 循环遍历 data 中每一行的第一个值,即 8 个值。如果您详细说明您正在尝试做的事情,帮助会更容易。
    • 我要做的就是这样
    猜你喜欢
    • 2018-03-20
    • 2013-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2012-12-29
    • 2014-09-25
    相关资源
    最近更新 更多