Output Argument Error - How to Define The Output Argument for a Function
Mostrar comentarios más antiguos
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. :/
Respuestas (1)
James Tursa
el 26 de Oct. de 2016
Editada: James Tursa
el 26 de Oct. de 2016
0 votos
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 comentarios
Mel Smith
el 27 de Oct. de 2016
Steven Lord
el 27 de Oct. de 2016
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
el 27 de Oct. de 2016
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.
Categorías
Más información sobre Whos en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!