- Instead of pre-allocating “elProdTotArS” and “savedEl” with a fixed size of 10, it can be calculated using the actual number of years present in the data using:
Index exceeds Matrix dimensions
9 ビュー (過去 30 日間)
古いコメントを表示
Hi!
I am trying to run this script to calculate different values for an assignment. Ive gotten most of it to work, but it is now stuck on line 162 when i run it, and tells me "index exceeds matrix dimensions". I have tried to compare the two matrixes i am trying to combine, and one of them seems to have a lot of zeros in it. Is there any way to elegantly fix this problem, and get rid of the index error?
The files "stralning" and "temperatur" are the text files that i am getting my data from, just for reference.
Thank you for any help!
0 件のコメント
回答 (1 件)
Abhishek
2025 年 4 月 30 日
It looks like some indices contained in the code are outside the bounds of indices for “elProdTotArs”. Moreover, there are some typos and errors in the code. The error provided in the question means that the index “f” is going above the size of “elProdTotArS”, which has only 10 rows as can be seen at line number 25 (“elProdTotArS = zeros(10,2);” ).
To address this, please make the following adjustments in the code:
years = stralning.data(end,1) - stralning.data(1,1) + 1;
elProdTotArS = zeros(years,2);
savedEl = zeros(years,2);
2. The while loop condition at line number 166 should be updated to prevent the index from exceeding the array size:
while elProdTot(k,1) == g && f < length(elProdTotArS)
3. There was a mismatch in variable names (“sparadEl “ vs “savedEl”). It should be corrected:
plot(savedEl(:, 1), savedEl(:, 2), 'o');
By making the above changes, the code is now able to handle cases, irrespective of any number of years in the data gracefully. Hope it helps.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!