The end operator must be used within an array index expression

My output is showing "The end operator must be used within an array index expression." when I try to run this:
%15. split spectograms of background noise between the training, validation, and test sets
numTrainBkg = floor(0.85*numBkgClips);
numValidationBkg = floor(0.15*numBkgClips);
XTrain(:,:,:,end+1:end+numTrainBkg) = Xbkg(:,:,:,1:numTrainBkg);
YTrain(end+1:end+numTrainBkg) = "background";
XValidation(:,:,:,end+1:end+numValidationBkg) = Xbkg(:,:,:,numTrainBkg+1:end);
YValidation(end+1:end+numValidationBkg) = "background";
Please help me solve this..

6 Comments

@Sharena Natasha Nor Hisham: please show us the complete error message. This means all of the red text.
@Stephen This is the whole red text shown:
The end operator must be used within an array index expression.
Please run the following command from within the scope where these variables are defined (i.e., I don't know if this is in a function or a script; if it's a function put this line in the function after where the variables are defined and before where the error happens; if it's a script you can just run this line on the MATLAB command prompt):
whos XTrain YTrain XValidation YValidation Xbkg
and show the output. This will tell us what type of variables these are. It seems like at least one of them is not an array.
@Benjamin this is the output:
Name Size Bytes Class Attributes
XTrain 98x50x1x170 3332000 single
XValidation 98x50x1x30 588000 single
Xbkg 98x50x1x200 3920000 single
YTrain 2856x1 4534 categorical
YValidation 1064x1 2742 categorical
but now i'm facing another issue after I tried training the network:
Error using trainNetwork (line 184)
Number of observations in X and Y disagree.
Please create a new question for this as it is unrelated.

Sign in to comment.

 Accepted Answer

You cannot use end in a variable that has not yet been created. Here, you are using end in the assignment to all your variables. Since they don't exist yet, you get an error. Consider this example.
a(end)=1
The end operator must be used within an array index expression.
Perhaps you meant to do this:
numTrainBkg = floor(0.85*numBkgClips);
numValidationBkg = floor(0.15*numBkgClips);
XTrain = Xbkg(:,:,:,1:numTrainBkg);
YTrain(1:numTrainBkg) = "background";
XValidation = Xbkg(:,:,:,numTrainBkg+1:end);
YValidation(1:numValidationBkg) = "background";
I've never used it on a 4D array, but you may want to look into using cvpartition.

1 Comment

Ahh I understand it now. I tried running the command you gave. And it worked! Thank you so much

Sign in to comment.

More Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!