How to use default values with deal()?

2 ビュー (過去 30 日間)
Mr M.
Mr M. 2014 年 11 月 14 日
コメント済み: Guillaume 2014 年 11 月 15 日
I use the fallowing method in a function to assign parameter values:
parameters = [1,2,3];
temp_param = num2cell(parameters);
[p1,p2,p3] = deal(temp_param{:});
It is possible to use default values somehow (with a simple compressed syntax)?
So if parameters = [1,2] I still want to use [p1,p2,p3] = ... but let p3=0 by default.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 11 月 14 日
編集済み: Azzi Abdelmalek 2014 年 11 月 14 日
parameters = [1 2];
temp_param = num2cell(parameters)
temp_param(numel(temp_param)+1:3)=num2cell(zeros(1,3-numel(temp_param)))
[p1,p2,p3] = deal(temp_param{:})
  2 件のコメント
Jan
Jan 2014 年 11 月 15 日
編集済み: Jan 2014 年 11 月 15 日
Or simpler:
temp_param(numel(temp_param)+1:3) = {0};
This works also, if the wanted length is larger.
Guillaume
Guillaume 2014 年 11 月 15 日
Note that similar to your previous question, the deal is completely unnecessary.
[p1, p2, p3] = temp_param{:};

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by