Function returning two vectors

조회 수: 18 (최근 30일)
TheFarmer
TheFarmer 2011년 6월 5일
Couldn't really find anything on google by searching for 'functions' and 'vectors' etc, so I'll try here.
If I needed a function returning two vectors (of different size), like this:
[vec1, vec2] = functionname(variables,...)
what is the best approach? If I try to transpose them I get an error since the dimensions don't agree, and if I return them as a vector that would make it difficult to get all the elements (unless there is some trick I have not thought about). Both vectors are about 50 elements long.
Thanks.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 6월 5일
People do that everyday using Matlab. Copy the following into a file called MyFun.m
function [y1,y2]=MyFun(x1,x2)
% input argument x1 and x2 are not used in this example
y1=rand(1,50);
y2=1:100;
Then, in Matlab command window, type
[z1,z2]=MyFun(1.1,2.2)
You get two return vectors in different size.
  댓글 수: 1
Matt Fig
Matt Fig 2011년 6월 5일
Even MATLAB functions do this sometimes:
[K,H,J] = unique(round(rand(1,6))) % Compare lengths of H and J.

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

추가 답변 (1개)

TheFarmer
TheFarmer 2011년 6월 5일
That was far more simple than I thought it would be! Thank you! :)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by