assigning multiple outputs from a vector

9 ビュー (過去 30 日間)
Victor
Victor 2014 年 3 月 11 日
コメント済み: Victor 2014 年 3 月 12 日
If I have a vector of a known length, let's say X = [ 1 2 3 4 ], is there an easy way to assign the elements of the vector to different variables? For example, I know I can do:
a = X(1); b = X(2); c = X(3); d = X(4);
Is there a function that will allow me to do something like [a b c d] = X? (I know this doesn't work, its just an example of what I am trying to do).

採用された回答

Matt J
Matt J 2014 年 3 月 11 日
Xcell=num2cell(X);
[a,b,c,d]=deal(Xcell{:});
  3 件のコメント
Matt J
Matt J 2014 年 3 月 11 日
編集済み: Matt J 2014 年 3 月 11 日
No, there is no way.
But you can use the function below to encapsulate everything into one function call [a,b,c,d]=vout(X)
function varargout=vout(C)
%
%Takes the elements of a cell or numeric array and assigns them to separate varargout
%
% [argout1,argout2,...]=vout(X)
%
if isnumeric(C), C=num2cell(C); end
C=C(:).';
Victor
Victor 2014 年 3 月 12 日
Thanks, Matt. That worked great.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by