필터 지우기
필터 지우기

Why does a function that should return 3 vectors arranged in a matrix only return 1 vector?

조회 수: 2 (최근 30일)
Consider a function:
function [a, b, c] = calculate(d, e, f)
This function should return vectors a, b and c - and as I see it, a, b and c should be arranged in a matrix. However, when I run this function, it returns only one vector! How can this be?

답변 (1개)

Matt J
Matt J 2016년 12월 29일
편집: Matt J 2016년 12월 29일
Call the function with all of its output arguments,
[a, b, c] = calculate(d, e, f);
and then if you want them in a matrix, concatenate as follows
matrix = [a,b,c]
The concatenation can only work if a,b,c are all column vectors of the same length, obviously.
and as I see it, a, b and c should be arranged in a matrix
No idea why you expect that. What if a,b,c are of different lengths? What if they aren't even all vectors? What if some are structs and others are strings? How would you define the concatenation, then?
  댓글 수: 2
Luki
Luki 2016년 12월 29일
allright, thx! It works, when I set up the variable "matrix" and let my function return it. I just figured, since in my function declaration it says:
function [a,b,c] = ...
that [a,b,c] would be the matrix returned by the function.
Image Analyst
Image Analyst 2016년 12월 29일
No, the brackets don't indicate that the 3 outputs will be somehow made into an array, they are just for collecting/indicating that the variables are a group of output arguments that are, or can be returned to the calling routine. It just sort of organizes all the output variables into a part of the line of code to distinguish/separate it from the "function" keyword, the equal sign, the function name, and the list of input arguments. Maybe/perhaps they could have designed the language to do it without the brackets, but for whatever reason they didn't so that's just they way they chose to do it.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by