Index in position 1 is invalid. Array indices must be positive integers or logical values?

1 view (last 30 days)
>> 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 Comments
tmarske
tmarske on 10 Mar 2020
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
Walter Roberson on 10 Mar 2020
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.

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 10 Mar 2020
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.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!