필터 지우기
필터 지우기

Please HELP!! Am I nuts?

조회 수: 1 (최근 30일)
Ali
Ali 2012년 11월 7일
I define a function and save it:
function [new_x, new_y] = TestArray(x, y)
new_y = y.^2;
new_x = x;
end
At cursor I type:
x = 1:10
y = 1:10:30
Answer = TestArray(x, y)
I get:
Answer =
1 2 3 4 5 6 7 8 9 10
How on earth is this possible? I am feeling tired but I can only assume I am going crazy, tiredness cannot be a reason for this level stupidity?! Shouldn't i return an matrix with size [2x10]?

채택된 답변

Image Analyst
Image Analyst 2012년 11월 7일
[new_x, new_y] in the function definition, or when you actually call this function, does not mean that you will get one variable out with new_x in column 1 and new_y in column 2, like you would if you did this:
c = [columnVector1 columnVector2];
It behaves differently. When calling or defining a function, it means that you get two variables returned and they are separate - not concatenated. If you specify only one, it will give you the left most return variable and any to the right of it are "thrown away" because you did not supply a variable in your calling routine to accept them. I hope that explains it.

추가 답변 (2개)

Friedrich
Friedrich 2012년 11월 7일
편집: Friedrich 2012년 11월 7일
Hi,
this behavior is correct. In a function the output arguments aren't concatinated. They are returned as several outputs.
Try calling your function TestArray as
[Answer, Answer2] = TestArray(x, y)
If you want to have a 2x10 Matrix as output, then you need to concatinate it yourself inside the function.

Ali
Ali 2012년 11월 7일
Thanks! I am surprised I haven't run into this before.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by