MATLAB Error: Array Indices must be positive or logical values

13 ビュー (過去 30 日間)
Emily Pich
Emily Pich 2019 年 3 月 29 日
コメント済み: Walter Roberson 2022 年 5 月 20 日
h=input('Enter estimate for root');
f=(h^3)-(9*(h^2))+3.8197;
fprime=(3*(h^2))-(18*h);
for i=1:10
h=h(i)+abs((f(h))/(fprime(h)));
end
  2 件のコメント
Adam
Adam 2019 年 3 月 29 日
You either want to create f and fprime as function handles to use the syntax you use, which seems un-necessary here, or just get rid of the (h) from within the for loop:
for i=1:10
h=h(i)+abs(f/fprime);
end
madhan ravi
madhan ravi 2019 年 3 月 29 日
Shot in the dark:
h=input('Enter estimate for root');
f=@(h)(h^3)-(9*(h^2))+3.8197;
fprime=@(h)(3*(h^2))-(18*h);
for i=1:10
h=h+abs((f(h))/(fprime(h)));
end

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

採用された回答

Walter Roberson
Walter Roberson 2019 年 3 月 29 日
Unless the user deliberately entered an anonymous function in respond to the input query, then after the input statement, h is going to be numeric, typically a scalar but potentially a vector or array.
If the user entered a vector or non-square array, then because h^3 would be matrix exponent, equivalent to h*h*h, you would get errors about non-square matrices. So if we made it through the assignment to f then we know h was either scalar or a square array, and f will be the same size. Then fprime would be that same size, scalar or square array.
Then we loop, and in that loop we attempt to access h(i) where i is up to 10. If h was entered as a scalar we need to be wary that h(2) through h(10) might not exist, so we need to pay attention to whether the rest of the expression computes a scalar (in which case h will not get extended) or a vector or array (in which case it will). Even if h was entered as an array, do we know that the array has at least 10 elements?
Then in the expression we take f(h) . We established that f is a numeric scalar or square array the same size as h is. f(h) is therefore an indexing request that can only work if the user happened to enter h as a set of positive integers none of which exceed the number of entries that the user entered for h.
Likewise, we established that fprime is a numeric scalar or square array the same size as h is, so fprime(h) would also be an indexing request, positive integers, blah blah blah.
The / operator is "matrix right division". With two numeric scalars or two square arrays of the same size on both sides, then the size of the output will be the same as the size of the input (which would not be the case if they were non-square arrays.)
We can be certain that if the user did happen to enter positive integers for h, those permitting f(h) and fprime(h) to be meaningful, that the calculation f(h)/fprime(h) will not have produced only integers. So if we managed to get this far on interation 1, then the abs() is going to produce a numeric scalar or square array the same size as h was, and we add h(1) and the result is going to be a numeric scalar or square array the same size as h was.
If we made it through this, then we loop around, try to access h(2), definitely fail if the user entered a numeric scalar for h. If the user entered a square array for h (other than 1 x 1) then h(2) will exist, but there is no realistic chance that it will contain only positive integers that can be used as indices to the arrays f and fprime.
So, what went wrong? This:
You confused expressions with formulas.
In virtually all programming languages, when you have an assignment of the form
variable = expression involving variables
then unless the expression has a special form (exact form varies with the language), then at the time the statement is evaluated, a copy is taken of all numeric variables involved in the expression as of the time the statement is evaluated, and a result is computed, and the results are assigned to the variable, with all "history" of how the variable was computed being discarded. Then if the variables involved in the expression change values afterwards, then the value of the computed variable does not change.
A = 1
B = A + 1 %B becomes 1 + 1 = 2
A = 5
at this point B is still 2, and does *not* suddenly evaluate to 5+1 = 6
MATLAB suports the Symbolic Toolbox, which provides some ways to create formulas:
syms A
B = A + 1 %B becomes the _formula_ A+1
A = 1
subs(B) %subs looks at B, sees it involves A,
% looks in the workspace and sees that A is 1, and drops it in,
% so 1 + 1 = 2 would be calculated
A = 5
subs(B) %subs looks at B, sees it involves A,
% looks in the workspace and sees that A is 5, and drops it in,
% so 5 + 1 = 6 would be calculated
but what you probably want is:
f = @(h) (h.^3) - (9*(h.*^2)) + 3.8197;
This creates an anonymous function of one input named h, that computes that expression, and the handle to the function is assigned to f. Then when you invoke f(something) then the function runs and computes the value you want.
  1 件のコメント
Emily Pich
Emily Pich 2019 年 4 月 1 日
Thank you so much for your extensive answer! You're a huge help.

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

その他の回答 (1 件)

Preksha Gupta
Preksha Gupta 2022 年 5 月 20 日
load Meter1Cor.txt
XTest= Meter1Cor(:, 2:10);
YTest= Meter1Cor(:,11);
XTest=XTest';
YTest= YTest';
load one
y=one(XTest)
error=y-YTest;
plot(error,'g')
title("Error");
xlabel("Epochs");
ylabel("Error");
This is for testing a trained neural network. "one" is name of trained neural network and i am receiving an error stating that "array indices must be positive intergers or logical values." What could be a possible mistake i made?
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 5 月 20 日
XTest= Meter1Cor(:, 2:10);
That creates a 2d array that would not typically be restricted to positive integers.
load one
y=one(XTest)
You load a variable and then you try to index it using linear indices whose values are the contents of XTest. That requires that XTest either be empty or be all positive integers.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by