Attempted to access solutionsofar(:,0); index must be a positive integer or logical.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Retam Paul
am 20 Feb. 2019
Bearbeitet: DGM
am 24 Okt. 2024
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 Kommentare
Akzeptierte Antwort
Omer Yasin Birey
am 20 Feb. 2019
Verschoben: DGM
am 23 Okt. 2024
You have to assign something into solutionsofar, apparently it has 0 element.
1 Kommentar
DGM
am 23 Okt. 2024
Bearbeitet: DGM
am 23 Okt. 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.
Weitere Antworten (1)
Steven Lord
am 23 Okt. 2024
Verschoben: DGM
am 24 Okt. 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 Kommentare
Steven Lord
am 23 Okt. 2024
Verschoben: DGM
am 24 Okt. 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.]
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!