Error: index must be a positive integer or logical.

22 ビュー (過去 30 日間)
ERFAN AKBARI
ERFAN AKBARI 2019 年 5 月 22 日
コメント済み: ERFAN AKBARI 2019 年 5 月 23 日
Hi dear friends.
I am matlab beginer and have a problem about the Error: index must be a positive integer or logical.
That I see in this part of my code.
The error is: Attempted to access Bs(0); index must be a positive integer or logical.
So I wana have a negative value in Bs(0). what should I do?
My code is:
yo=0.1+1.5*i;
Bs=zeros(1,10);
Bs(0)=-(imag(yo));

採用された回答

Walter Roberson
Walter Roberson 2019 年 5 月 22 日
You are confusing formula and indexing.
In MATLAB, except under some circumstances involving symbolic variables, every time you have () appearing after a variable on the left hand side of an assignment statement, you are doing indexing. Bs(0) is an attempt to index a variable named Bs at offset 0. That is not permitted in MATLAB: offsets must be positive integers, so 1, 2, 3, ...
In particular assigning to Bs(0) does not create a formula for Bs saying that when formula Bs is evaluated at value 0, that a particular value is to be result.
MATLAB has two basic types of objects: 1) variables that can be indexed at positive integer locations (or by logical values), and 2) formula that can be evaluated at arbitrary values (that need not even be real-valued.)
Formulas in MATLAB are called functions. When you can write them in the form of single expressions, you can create them as "handles" to "anonymous functions" and you can assign the handles to variables. For example,
F = @(x) x.^2 - 1
creates an anonymous function with formula x -> x^2-1, and assigns the handle of that function to F. After that, F(-7.2) and F(0) and F(pi) would all be valid invocations of the function handle to compute the value of the formula.
In MATLAB, you cannot define a piece by piece the way you might abstractly write y(-1) -> 5, y(0) -> -2, y(pi) -> 11, y(pi+1E-50) = -4 . There is no way in MATLAB (without writing a bunch of code yourself) to make a statement along the line of
%this section is not valid in MATLAB
Y(-1) = 5;
Y(0) = -2;
Y(pi) = 11;
Y(pi+1E-50) = -4;
to try to define a formula for Y. There are ways you can express computing this, such as
Y = @(x) (x==-1).*5 + (x==0).*-2 + (x==pi).*11 + (x==pi+1e-50).*-4
Y = piecewise(x==-1, 5, x==0, -2, x==pi, 11, x==pi+1e-50, -4) %requires symbolic toolbox
Your line of code
Bs=zeros(1,10);
is creating Bs as a variable that holds 10 (double precision) values and is initialized to all 0. Variables can be indexed at positive integer locations, so Bs(1) = -(imag(yo)); would be valid, but this is not a formula.
  1 件のコメント
ERFAN AKBARI
ERFAN AKBARI 2019 年 5 月 23 日
Thank you very much for your answering.

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2019 年 5 月 22 日
編集済み: John D'Errico 2019 年 5 月 22 日
You wrote this:
Bs(0)=-(imag(yo));
MATLAB is not whatever other language you came from. MATLAB has an index origin of 1, NOT 0. That means the first element of an array is indexed with 1.
This is a basic thing, something you would learn in the getting started tutorials.
  1 件のコメント
ERFAN AKBARI
ERFAN AKBARI 2019 年 5 月 23 日
Thank you Man.
I confused it with C language

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by