Attempted to access solutionsofar(:,0); index must be a positive integer or logical.
조회 수: 2 (최근 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
댓글 수: 0
채택된 답변
Omer Yasin Birey
2019년 2월 20일
이동: DGM
2024년 10월 23일
You have to assign something into solutionsofar, apparently it has 0 element.
댓글 수: 1
DGM
2024년 10월 23일
편집: DGM
2024년 10월 23일
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
2024년 10월 23일
이동: DGM
2024년 10월 24일
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
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
2024년 10월 23일
이동: DGM
2024년 10월 24일
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.]
참고 항목
카테고리
Help Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!