For loop not working when run inside a function!

조회 수: 2 (최근 30일)
EB
EB 2017년 4월 20일
댓글: Jan 2017년 4월 20일
I have a for loop inside a function Function. When I call that function within a script I get an error Index exceeds matrix dimensions and it points to a specific line of Function. This line of code that is pointed out corresponds to a for loop.
The for loop is built for following case:
I have two matrices of size.
B (400x30)
C (1x30)
From these matrices I need to get a matrix A (400x30) that is obtained in the way
A(1,1) = B(1,1)*C(:,1)...A( 1,30) = B( 1,30)*C(:,30)
A(2,1) = B(2,1)*C(:,1)...A(400,30) = B(400,30)*C(:,30)
The for loop I wrote in order to get A is following:
for p = 1:400;
for i = 1:30;
A(p,i) = C(p,i)*B(:,i);
end
end
Calling the function Function within a script I get the error, but if I go inside the function and evaluate the lines of code line by line, I don't get any error for the for loop, I can calculate the A matrix easily.
I really don't understand why is this happening, and I need to call Function within a script? But with getting this error constantly I can not move further with the code.
Many thanks,

채택된 답변

Jan
Jan 2017년 4월 20일
This sounds like magic:
Calling the function Function within a script I get the error,
but if I go inside the function and evaluate the lines of code
line by line, I don't get any error for the for loop.
Matlab does not do any magic. It is not getting clear to me, when the problem occurres, but you can be sure that there is a deterministic resaon for this. You can use the debugger to see, what happens. Type this in the command window:
dbstop if error
Now run the code again. If Matlab stops at the error, check the sizes of the used variables in the failing line:
% A(p,i) = C(p,i)*B(:,i); % Assumed offending line
p
i
size(C)
size(B)
But perhaps the error appears anywhere else. This would be clear, if you post the complete error message, not just a part of it.
  댓글 수: 2
EB
EB 2017년 4월 20일
The code is long, I have many functions to call, and this was just a small piece of it. But,
dbstop if error
helped me to see where I did the mistake.
The for loop is actually ok, but I made a msitake on other place. The arguments I called inside Function within the script weren't written in the same order as in the function file Function itself and then I had a mess in size of matrices. But, now everything's fine.
Thank you!
Jan
Jan 2017년 4월 20일
@EB: You are welcome. The debugger is the best friend of the programmer! :-)

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

추가 답변 (2개)

Torsten
Torsten 2017년 4월 20일
편집: Torsten 2017년 4월 20일
A(p,i) = C(1,i)*B(p,i);
Best wishes
Torsten.
  댓글 수: 2
EB
EB 2017년 4월 20일
It's not working. I get the same error Index exceeds matrix dimensions when I call the function within the script. But evaluating the for loop directly inside the function it works.
Andrei Bobrov
Andrei Bobrov 2017년 4월 20일
Please attach your function and script

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


Andrei Bobrov
Andrei Bobrov 2017년 4월 20일
If we have two matrices of size: B (400x30) C (1x30):
A = B.*C; % in R2016b and later
A = bsxfun(@times,B,C); % other early versions

카테고리

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