Index in position 1 exceeds array bounds (must not exceed 1).
4 views (last 30 days)
Show older comments
Hello, I went through the other queries under the same error. I got the what it means, but still dont understand in my case why does it show up.
Does it have any other reason or I am still making the same mistake. Could anybody please check the following code and help me?
%Some code earlier which produces the arrays WCM, WBM, KSM, OTM, BTM, WMM. These array are converted to the matrix of size 10 X 86400.
% But in Input file- if any of the input variables (WC,WB,KS,WM,BT,OT) are absent then the corresponding variables i.e. WCM, WBM etc.are set to zero.
% NumWC, NumWB etc. are set to by default valu of 1.
if isempty(WCM)
WCM=0;
end
if isempty(WBM)
WBM=0;
end
if isempty(KSM)
KSM=0;
end
if isempty(BTM)
BTM=0;
end
if isempty(OTM)
OTM=0;
end
if isempty(WMM)
WMM=0;
end
for i=1:10
All(i,:)=NumWC.*WCM(i,:)+NumWB.*WBM(i,:)+NumKS.*KSM(i,:)+NumBT.*BTM(i,:)+NumOT.*OTM+NumWM.*WMM;
end
% When any I input the file with First four variables (WC,WB,BT,KS), there is no problem. I get the value of All.
% But when I input the file with only two variables (WC and WB) or with only one variable (WC), I get the error "Index in position 1 exceeds array bounds (must not exceed 1)."
All the variables (WCM,WBM...etc) are supposed to be of size 10 x 86400. and NumWC, NumWB.... etc are scalar values.
When I have only WC or WC and WB as my input variables, I get the error. Could you help me knowing what I am missing?
0 Comments
Accepted Answer
Harikrishnan Balachandran Nair
on 24 Sep 2021
Edited: Harikrishnan Balachandran Nair
on 24 Sep 2021
Hi,
I understand that you are encountering an error which says " index in position 1 exceeds array bounds" , on running your code.
In the code that you have provided, you have set your input variable to '0' incase it is an empty array. This sets the input to a 1*1 double array. When you try to access the 'ith' row in the corresponding input, you will face an indexing error, when i is greater than 1.
You can resolve this issue by using the 'zeros' function in matlab to initialize the input as a 10*86400 array of zeros, which is the dimension that you are expecting. You may refer to the following line of code.
if isempty(WCM)
WCM=zeros(10,86400);
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!