index exceeds matrix dimension

조회 수: 1 (최근 30일)
Osita Onyejekwe
Osita Onyejekwe 2017년 7월 17일
편집: Jan 2017년 7월 18일
I am calculating mean squared errors for 5 different methods using 3 different sigmas. Each Method is going to be a composition of a 5 X 3 matrix. For hours I have been trying to figure this out but I cannot possibly figure out what I am doing wrong. I keep getting this error:
Index exceeds matrix dimensions.
Error in AR_1_2 (line 61)
MSE_CV(iLoop,:) = (1/length(ycnoise))*sum((yHatCV(iLoop,:) - origFun).^2);
HERE IS THE CODE BELOW
*****************************************************************************
for iLoop = 1:5
MSE_CV = zeros(5,length(sigma));
MSE_plugIN = zeros(5,length(sigma));
MSE_Mean_Max_SNR = zeros(5,length(sigma));
MSE_maxSNR = zeros(5,length(sigma));
MSE_Poly_SNR = zeros(5,length(sigma));
%for j = 1: length(sigma)
MSE_CV(iLoop,:) = (1/length(ycnoise))*sum((yHatCV(iLoop,:) - origFun).^2);
MSE_plugIN(iLoop,:) = (1/length(ycnoise))*sum((yHatPlugIn(iLoop,:) - origFun).^2);
MSE_Mean_Max_SNR(iLoop,:) = (1/length(ycnoise))*sum((yHatSNRmeanBW(iLoop,:) - origFun).^2);
MSE_maxSNR(iLoop,:) = (1/length(ycnoise))*sum((yHatSNRAll(iLoop,:) - origFun).^2);
MSE_Poly_SNR(iLoop,:) = (1/length(ycnoise))*sum((yHatSNRfinalBW(iLoop,:) - origFun).^2);
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2017년 7월 18일
What is size(yHatCV) ? And is it possible that you have a variable named "sum" ?
Image Analyst
Image Analyst 2017년 7월 18일
Since you're creating new versions of those arrays at the beginning of each iteration, only the iLoop row will get set. After the loop is done only row 5 of all the matrices will have anything in them. Maybe you want the zeros() preallocation before the loop even starts.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2017년 7월 18일
편집: Jan 2017년 7월 18일
Use the debugger to identify the problem. Type this in the command window
dbstop if error
or use the corresponding menu in the editor. Then run te code again until it stops at the error. Now check the locally used variables and functions either in the CommandWindow or in the WorkSpace browser:
% MSE_CV(iLoop,:) = (1/length(ycnoise))*sum((yHatCV(iLoop,:) - origFun).^2);
iLoop
size(MSE_CV)
which('length') % Function shadowed by local variable?
length(ycnoise)
which('sum') % Function shadowed by local variable?
yHatCV(iLoop,:)
sum((yHatCV(iLoop,:) - origFun).^2)
The debugger is the best friend of the programmer. Learn how to use it efficiently.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by