필터 지우기
필터 지우기

How to call and also return a matrix in a function?

조회 수: 14 (최근 30일)
Khushank Singhal
Khushank Singhal 2017년 12월 10일
답변: KL 2017년 12월 11일
I have a certain requirement in my code that I need to define a separate function for computation of a variable. The variable is a matrix of order of 32x32. As inputs to the function, there are two different matrices of the same order. After calling the function in the code, I want the function to return the whole matrix of order 32x32.
This is how I am writing it currently (but it is not correct):
-calling the function :
matrix = variable(input1, input2);
where 'matrix' is the output and 'variable' is the function name. 'input1' and 'input2' are also matrices of order 32x32.
-function:
function output = variable(in1, in2)
I wanted to know the correct method to do this.
The error that I am getting:
Output argument "output" (and maybe others) not assigned during call to "variable".
Error in test (line 108)
matrix = variable(input1, input2);
  댓글 수: 3
Matt J
Matt J 2017년 12월 10일
편집: Matt J 2017년 12월 10일
There is no error in what you have shown so far, but you haven't shown very much. What is the body of the function variable() doing and what is the error message?
Stephen23
Stephen23 2017년 12월 11일
"This is how I am writing it currently (but it is not correct)"
"I wanted to know the correct method to do this."
What you have shown us is correct. If you are getting any error message then please show us the complete error message (this means all of the red text).

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

채택된 답변

KL
KL 2017년 12월 11일
Output argument "output" (and maybe others) not assigned during call to "variable"
Just as it says you probably haven't assigned anything to output. For example, if you want to add in1 and in2 to form output, it should be,
function output = variable(in1,in2)
output = in1+in2;
end
Passing a matrix is just like passing any other variables into a function but just that you have to know their sizes to do any computations on it. Here I assumed both are same size and so the output will be of their size as well.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by