My code is generating this error " Index exceeds matrix dimensions" . Apparently it looks fine. Still can not figure out the problem.

c1 = 0.1;
c2 = 0.0001;
L = 5.0;
M = 0.04;
P1 = 1.0e-2;
P2 = 1.0e-5;
for i=1:64
for j=1:64
C1(i,j) = c1 + P1*(0.5-rand);
C2(i,j) = c2 + P2*(0.5-rand);
end
end
for i=1:64
for j=1:64
dfdC1(i,j) = (L/M) - M*(log(1 - C1(i,j) - C2(i,j)) - log(C1(i,j)))/M;
end
end

 채택된 답변

dfdc1(i,j) = (L/M) - M*(log(1 - c1(i,j) - c2(i,j)) - log(c1(i,j)))/M;
c1 and c2 are defined as single values (0.1 and 0.0001), but you are trying to index them. I am assuming you want to refer to C1 and C2.
dfdc1(i,j) = (L/M) - M*(log(1 - C1(i,j) - C2(i,j)) - log(C1(i,j)))/M;

댓글 수: 5

Well spotted.
A good example of why having names that differ only by case is considered a bad practice.
Yeah, you are right. Thanks man. Its working now.
Yeah, i just realize that too. I am new to programming and learning it by myself so bad practices are very rampant in my codes, i guess.
No problems. I have done the same thing, even when I don't have c1 and C1 as different variables. Sometimes you just need an extra set of eyes.
I wish Matalb offers an intellegent code analyzer which can recommend these kind of issues and can present alternate solutions as well.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2019년 9월 16일

댓글:

2019년 9월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by