How to set multiple values at the same time from a vector?

2 次查看(过去 30 天)
There is a vector v = [1,2,3], and I want to set a=1, b=2, c=3 in a compact form. I tried [a,b,c] = deal(v), but it is not working, because the result is a = [1,2,3]. deal(1,2,3) is ok, but I dont know how to make 1,2,3 from [1,2,3]. In Maple od(v) makes this. Is there any solution for this in MATLAB?

采纳的回答

Manoj
Manoj 2014-11-14
This is one way I could think of , but maybe there is an easier way
v=[1,2,3];
vv=num2cell(v);
[a,b,c]=deal(vv{:});
  7 个评论
Mr M.
Mr M. 2014-11-27
OK, thanks: [a,b,c]=deal(vv{:}); is a solution. But what if I don't know the number of parameters? For example the maximum is three (a,b,c) but v maybe contains only two, and I want to set c to a default value in this case. How to solve this problem?
Guillaume
Guillaume 2014-11-28
As I said, you don't need the deal,
[a,b,c] = vv{:};
works just as well.
As for your other question, please start a new question so that whoever answers it can get the credits for it.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by