Error: Index exceeds array bounds.

3 views (last 30 days)
Maryam Idrees
Maryam Idrees on 16 Nov 2021
Answered: Sudharsana Iyengar on 16 Nov 2021
Hi,
I have read multiple posts related to the subject error but couldn't find the answer related to my query. Any help is much appreciated.
I have an excel file (256x2) and I am reading it in MATLAB with xlsread. The task is to find a number (coming from an input data array) in the first column and locate its neighbouring second column entry. I am doing it by following way.
V = Excel_file(Excel_file(:,1) == Column1(i),:);
modTemp=V(2);
After locating the respective entry, subtract it from 256 and store the result in mod_cal variable which is also declared as an array.
mod_cal(i)=256-modTemp;
The code is doing fine uptill here. Problem begins in the next part. I have to find the mod_cal variable's value in the second column now and whatever its respective neighbouring entry would be in the first column, i need to read it in an array named mod_MulInverse and that is going to be my result.
W = Excel_file(Excel_file(:,2) == mod_cal(i),:);
mod_MulInverse(i)=W(1);
Complete code is as follows.
mod_cal=zeros(1, 256);
mod_MulInverse=zeros(1, 256);% for storing the values with 256-col2
Excel_file = xlsread('powerValues.xlsx');
for i=1:256 % see excell, we want to go from 0 to 256 (Exclude first 2 entries)
if(Column1(i)==0 || Column1(i)==1) %OR condition
mod_cal(i)=Column1(i);
mod_MulInverse(i)=Column1(i);
else
V = Excel_file(Excel_file(:,1) == Column1(i),:);
modTemp=V(2);
mod_cal(i)=256-modTemp; %256-alphapower
W = Excel_file(Excel_file(:,2) == mod_cal(i),:);
mod_MulInverse(i)=W(1);
end
end
The first part works fine, I get an error for the second part which says that the index exceeds array bounds for mod_MulInverse(i)=W(1) line.
Please help me understand the issue here and resolve the error.
Thank you !

Answers (1)

Sudharsana Iyengar
Sudharsana Iyengar on 16 Nov 2021
Can you check the Size of W. and see what W(1) contains.
mod_MulInverse(i)=Column1(i); %working
mod_MulInverse(i)=W(1);% not working Are you sure for all values you want to store W(1) only.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!