Output Argument Error - How to Define The Output Argument for a Function
이전 댓글 표시
I'm trying to create and use the following function:
function [dctmatrix]=dctmatrix(i,j,N)
if i==1
dctmatrix=sqrt(1/N);
elseif i>=2
dctmatrix=sqrt(2/N)*cos((pi*(2*j-1)*(i-1))/(2*N));
end
end
But whenever I try to use it as follows, I get this error:
i=1:512;
j=1:512;
Y=dctmatrix(i,j,512);
Yinv=transpose(dctmatrix(i,j,512));
X_gray1comp=Y*X_gray1*Yinv;
Output argument "dctmatrix" (and maybe others) not assigned during call to "dctmatrix".
Error in Project2code (line 100)
Y=dctmatrix(i,j,512);
I thought that since I said "dctmatrix=..." in the code for the function itself, that that counted as an output argument. I don't understand how what I've done is wrong. :/
답변 (1개)
James Tursa
2016년 10월 26일
편집: James Tursa
2016년 10월 26일
0 개 추천
Don't have the return variable name the same as the function name. Use a different name for the return variable (e.g., "result"). Also, you need to use vectorized calculations if you want your function to operate on non-scalar inputs for i and j. So you will need to adjust your i==1 and i>=2 tests to account for vector inputs, and you will need to adjust your cos(etc) calculation for vector inputs.
댓글 수: 3
Mel Smith
2016년 10월 27일
Steven Lord
2016년 10월 27일
The body of the if section will be executed only if ALL of the elements of i == 1 are true. You're calling this function with i = 1:512. Are ALL the elements of 1:512 equal to 1?
Similarly, the body of the elseif section will be executed only if ALL the elements of i >= 2 are true. Are ALL the elements of 1:512 greater than or equal to 2?
Walter Roberson
2016년 10월 27일
Suppose an input was less than 1, or was in the range (1, 2) exclusive such as sqrt(2), then you would not generate an output.
카테고리
도움말 센터 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!