Attempted to access solutionsofar(:,0); index must be a positive integer or logical.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Omer Yasin Birey
il 20 Feb 2019
Spostato: DGM
il 23 Ott 2024
You have to assign something into solutionsofar, apparently it has 0 element.
1 Commento
DGM
il 23 Ott 2024
Modificato: DGM
il 23 Ott 2024
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.
Più risposte (1)
Steven Lord
il 23 Ott 2024
Spostato: DGM
il 24 Ott 2024
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 Commenti
Steven Lord
il 23 Ott 2024
Spostato: DGM
il 24 Ott 2024
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.]
Vedere anche
Categorie
Scopri di più su Operators and Elementary Operations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!