error when call function
조회 수: 2 (최근 30일)
이전 댓글 표시
hello,
I use matlab for excercises on the university. I declare a function 'benadering_four' and when I ask for benadering_four(0) (so 0 put in the function) then it works but when I put a vector in the function (in the code, benadering_four(xx) ) then it gives me the error:
*Error using * Inner matrix dimensions must agree.
Error in @(y)[1,cos([1:floor(n/2)]*y),sin([1:floor(n/2)]*y)]
Error in @(y)Fourier(pi*y)*coef_four*
xx = -1:0.001:1;
n = 9;
coef_four = 1/pi*[pi*atan(5)/5; 1.08104; .552133; .30444; .157123; 0; 0; 0; 0]
Fourier = @(y) [1 cos([1:floor(n/2)]*y) sin([1:floor(n/2)]*y)]
benadering_four = @(y) Fourier(pi*y)*coef_four
benadering_four(0)
benadering_four(xx)
I doesn't understand why it works with an number and why it doesn't work with a vector. Can someone help me?
댓글 수: 0
채택된 답변
Adam
2014년 10월 30일
편집: Adam
2014년 10월 30일
cos([1:floor(n/2)]*y
will only be valid syntax for y being a vector in very specific circumstances - i.e. if they are the same length and one is a row vector, the other is a column vector. Depending which way round they are in that case you either get a scalar result or a square matrix result.
In all other cases you get a syntax error because you cannot do matrix multiplication (or indeed element-wise multiplication) on two arbitrary vectors of different lengths.
In this case I don't know what your expectations would be from passing a vector in. Do you expect a 3-element vector out? If so then y needs to be a column vector of length n.
댓글 수: 4
Adam
2014년 10월 30일
Try transposing xx or whatever you are passing in.
You can do matrix multiplication if you have a row vector and column vector but not two of the same.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!