Array not positive real value?
1 次查看(过去 30 天)
显示 更早的评论
Hello, I am having trouble plotting this data. I have the code below and I keep getting an error regarding "Array indices must be positive
integers or logical values."
syms x
fun = 1-(nu/2)*i*sin(x)-nu^2*sin(x/2)^2 -nu*sin(x/2)^2- nu/2*1i*sin(x)+(nu*(nu-1)/4)*(1-2*exp(-1i*x)+exp(-1i*2*x));
R = matlabFunction(real(fun), 'vars', x);
I = matlabFunction(imag(fun), 'vars', x);
range = [0:.01:pi];
z= [];
for i = 1:length(range)-1
j = i*.01;
R(i)
I(i)
z(1,i+1) = abs(fun(j)); %sqrt(R(j)^2+I(j)^2);
end
Any help would be apprecaited. Also, the commented out code is just a different method I was using earlier that does the same as the abs()
1 个评论
Geoff Hayes
2020-11-9
Braden - which line of code is generating the error? Typically, the "Array indices must be positive integers or logical value" error is telling you that you are using an invalid index into an array. From your code, it looks like i is always an integer and so the use of R(i), I(i), and z(1,i+1) are probably valid. Depending upon what fun is (a function? or an array?) then given that j is not an integer, fun(j)) may be invalid. (I don't have the symbolic toolbox so it isn't clear to me what fun would be.)
回答(1 个)
Athul Prakash
2020-11-12
As others have pointed out, this error message typically occurs when you're indexing an array with incompatible values (usually decimals, negatives or non-numeric types).
There seems to be a lot of indexing in the code attached; could you post the full error message, including the line where the error has occured?
At first glace, I'm not sure that 'fun' got assigned properly, there could be a bug. You may check that 'fun' works as expected before invoking 'fun(j)' later.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!