Matlab索引越界报错求助:id(90)访问超出numel(id)=89范围
Hey there, let's work through this error you're hitting. First, let's break down what the error message is actually saying:
Attempted to access id(90); index out of bounds because numel(id)=89.Error in Untitled66 (line 26) person = find(id(fileNum)==ids);
This is telling you that you tried to access the 90th element of the id array, but id only has 89 elements total. The problem triggers at line 26 when you run id(fileNum)—that means the variable fileNum has a value of 90, which is larger than the maximum valid index of id (MATLAB uses 1-based indexing, so an array with 89 elements only goes up to index 89).
Let's go through the most likely causes and fixes:
fileNumis set to an invalid value
Maybe you're incrementingfileNumin a loop, and it's going past the actual length ofid. For example, if you hardcoded a loop to run from 1 to 90 butidonly has 89 elements. To fix this, first check whatfileNumis right before line 26—adddisp(fileNum)above that line to see its value. Then adjust your loop to use the length ofidas the upper bound:for fileNum = 1:length(id) % Your code here endThe
idarray is shorter than you expected
Maybe you loaded or generatedidincorrectly, and it only has 89 elements instead of the 90 you assumed. Verify its length by runningnumel(id)orlength(id)in the command window. Double-check whereidcomes from—did you miss loading a data point, or is there a bug in the code that builds theidarray?You mixed up the indexing logic
Looking atperson = find(id(fileNum)==ids);—maybe you meant to compare the entireidarray to a single element fromids, instead of grabbing one element fromidto compare toids? Ifidsis an array and you want to find positions inidthat matchids(fileNum), the correct syntax would be:person = find(id == ids(fileNum));This way you're not indexing into
idwithfileNum, so you avoid the out-of-bounds error entirely (just make surefileNumstays within the length ofids).
Try these steps one by one, and you should get this sorted out quickly!
内容的提问来源于stack exchange,提问作者popeye

