Attempted to access solutionsofar(:,0); index must be a positive integer or logical.

26 次查看(过去 30 天)
function run_cont(fun,N)
global solutionsofar functionname
functionname=fun;
for k=1:N ig=2*solutionsofar(:,end)-solutionsofar(:,end-1); x=newton('continuationloop',ig); solutionsofar=[solutionsofar,x]; end
This gives me error Attempted to access solutionsofar(:,0); index must be a positive integer or logical.
Error in run_cont (line 8) ig=2*solutionsofar(:,end)-solutionsofar(:,end-1);
Please suggest me a fix. Thank You

采纳的回答

Omer Yasin Birey
Omer Yasin Birey 2019-2-20
移动:DGM 2024-10-23,0:39
You have to assign something into solutionsofar, apparently it has 0 element.
  1 个评论
DGM
DGM 2024-10-23,0:48
编辑:DGM 2024-10-23,0:50
You know what. This is now the answer to this question.
I know it might not have been intended, but I appreciate literalist framing of the response. It's a true statement, but the underlying lesson is that nobody can troubleshoot code unless you include it.
Mostly, I just got sick of seeing this maddeningly unanswerable dead question in the sidebar. Now it has an answer that it deserves.
PS: The fact that it's a global is just the cherry on top.

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2024-10-23,1:57
移动:DGM 2024-10-24,11:51
To clarify, most likely solutionsofar starts off as a 0-by-0 matrix because it was declared as global. From that documentation page:
"If the global variable does not exist the first time you issue the global statement, it is initialized to an empty 0x0 matrix.
If a variable with the same name as the global variable already exists in the current workspace, MATLAB issues a warning and changes the value of that variable and its scope to match the global variable."
So the first time this code is run, assuming the global variable didn't already exist, it would be initialized to a 0-by-0. Then when the code asks for solutionsofar(:, end), end evaluates to 0 and causes this error.
A = zeros(0, 0);
y = A(:, end) % error
Index in position 2 is invalid. Array indices must be positive integers or logical values.
The exact wording of the error message has changed since the question was originally asked.
Personally I'd fix this either by avoiding using global variables entirely or initializing it to contain a "dummy" solution (with at least two columns, since the code indexes into it asking for the end and end-1 columns.)
  3 个评论
Steven Lord
Steven Lord 2024-10-23,17:35
移动:DGM 2024-10-24,11:52
Would you like me to move my comment into a separate answer for this question? [Or since you have enough reputation, you could move it if you wanted.]
DGM
DGM 2024-10-24,11:51
编辑:DGM 2024-10-24,11:54
I'll move it and accept it if you rephrase it to recontextualize it as a standalone response. I'll rephrase my own lazy and delerious commentary on the original response as needed.
Moving comments out-of-sequence otherwise gets weird.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by