gpt4 book ai didi

Matlab:奇怪的尝试访问错误...索引必须是 pos。整数或逻辑

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:09 25 4
gpt4 key购买 nike

我对Matlab不是新手,但是在这种情况下我有一个以前没有遇到过的非常奇怪的错误。我试图制作我的代码的简化版本来演示我遇到的错误。基本上,我有索引错误,但正在访问的索引是一个正整数。

我想知道它是否与索引号的格式有关。无论如何,我们将不胜感激任何帮助或建议。

t=(0:0.00001:2*pi);
Cir(1,1:length(t)) = 0;
Cir(2,1:length(t)) = 0;
Cir(3,1:length(t)) = 0;

D0(1:length(t)) = 0;

% Search for the first minimum
[min1D0,t1Found] = min(abs(D0(1:length(t)/2)));
% Search for the second minimum
[min2D0,t2Found] = min(abs(D0(length(t)/2:length(t))));
t2Found = t2Found + length(t)./2; % Add tIndex/2 to correct the index

C1 = [Cir(1,t1Found),Cir(2,t1Found),Cir(3,t1Found)];
C2 = [Cir(1,t2Found),Cir(2,t2Found),Cir(3,t2Found)];

输出是:

Warning: Integer operands are required for colon operator when used as index 
Warning: Integer operands are required for colon operator when used as index
??? Attempted to access Cir(1,314161); index must be a positive integer or logical.

注意:我使用的是 R2009b。

最佳答案

length(t) 为 628319,为奇数。

您正在尝试访问

DO(length(t)/2)

其中 length(t)/2 是 314159.5。

数组索引必须是整数,这就是您收到该警告的原因。

t2Found 也会发生同样的事情。它不是整数。

t2Found = t2Found + length(t)./2;

结果为 314160.5。


作为旁注,您可以使用 zeros 函数进行预分配。

Cir = zeros(3, length(t));

会给你同样的结果。

关于Matlab:奇怪的尝试访问错误...索引必须是 pos。整数或逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14118072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com