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!