
Index in position 1 is invalid. Array indices must be positive integers or logical values?
조회 수: 1 (최근 30일)
이전 댓글 표시
>> lambda = 1;
>> phi = zero;
Undefined function or variable 'zero'.
>> phi = 0;
>> theta = 90;
>> B= 2*pi/lambda;
>> Lx = 20*lambda;
>> Ly = 10*lambda;
>> u = sin(theta);
>> v = 0;
>> f(u,v) = (sin((B*Lx)*u)/((B*Lx/2)*u))*(sin((B*Ly)*v)/((B*Ly/2)*v));
Index in position
1 is invalid. Array indices must be positive integers or logical values.
Hi,
After I try to get values to f(u,v) .I got this error
Index in position
1 is invalid. Array indices must be positive integers or logical values.
Could you please help solve this problem?
댓글 수: 3
tmarske
2020년 3월 10일
Here is the issue:
>> theta = 90;
...
>> u = sin(theta);
>> v = 0;
>> f(u,v) = (sin((B*Lx)*u)/((B*Lx/2)*u))*(sin((B*Ly)*v)/((B*Ly/2)*v));
Matlab interprets this last line as 'create a new array named f, evaluate the RHS of this expression, and assign that value to the (u, v) entry of the array f. Since neither u nor v are positive integers this is not possible, so Matlab throws the error.
It looks like what you actually wanted to do was create a new function named f. The syntax for this is different. You should either:
1) create a new function in a file as described here: https://uk.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html
2) create an anonymous function and assign it to f as follows:
>> f = @(u,v) (sin((B*Lx)*u)/((B*Lx/2)*u))*(sin((B*Ly)*v)/((B*Ly/2)*v));
Walter Roberson
2020년 3월 10일
Are you defining a formula, where you want to be able to provide arbitrary u and v afterwards and have it calculate the f value? Or are you defining an array, in which you have specific numeric u and v values and the result for the (J, K)'th pair of input values should be stored in output location indexed by (J, K)?
By the way: you divide by v but your v is 0, so your right hand side is going to be strictly inf or nan.
답변 (1개)
James Tursa
2020년 3월 10일
If you are trying to create an anonymous function, the syntax is:
f = @(u,v) (sin((B*Lx)*u)/((B*Lx/2)*u))*(sin((B*Ly)*v)/((B*Ly/2)*v));
Then downstream in your code you can call f(u,v) with arbitrary u and v inputs.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!