Index in position 1 exceeds array bounds (must not exceed 1).

12 views (last 30 days)
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?

Accepted Answer

Harikrishnan Balachandran Nair
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
  3 Comments
Harikrishnan Balachandran Nair
Please make sure that all the inputs are having 10 rows, and the number of columns are also same. If not , zero padding can be done to make the inputs to be of same dimension. The error should not occur if all your inputs are having 10 rows. Also, as per your code, if the inputs 'OTM' and 'WMM' are of dimension 10*86400, it will probably result in an error, as you are doing an element wise multiplication on the whole matrix on those two inputs, and hence the dimension on left and right hand side of the equation will not match.
Rohit Mangalekar
Rohit Mangalekar on 26 Sep 2021
Thank you sir. It worked. There was some syntax error. Thank you for your help.

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!