Attempted to access solutionsofar(:,0); index must be a positive integer or logical.
7 ビュー (過去 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!