在MATLAB中同时定义多个变量 [英] Define multiple variables at the same time in MATLAB
问题描述
欲同时以限定多个变量。
例如,我想定义
I want to define multiple variables at the same time. For example, I want to define
a = 1
b = 2
c = 3
这样的。
所以我做了矩阵[A,B,C]
:
x = [a, b, c];
y = [1, 2, 3];
x = y
所以我希望得到如下回答。
So I want to get the following answer.
a = 1
b = 2
c = 3
如果我用
[a, b, c] = deal(1, 2, 3)
那么,我可以得到
then, I can get
a = 1
b = 2
c = 3
不过,我想用矩阵 X
而不是 [A,B,C]
所以,如果我使用,
x = deal(1,2,3)
有一个错误。
请问有什么解决办法吗?
Is there any solution?
推荐答案
也许我不明白的问题,但如果你想使用矩阵 X
而不是 [A,b,C]
为什么你不把它定义为
Maybe I don't understand the question but if you want to use the matrix x
instead of [a, b, c]
why don't you just define it as
x = [1, 2, 3];
从你的问题听起来我,如果你是过于复杂的问题。你开始想通过申报
From your question it sounds to me as if you are overcomplicating the problem. You begin by wanting to declare
a = 1;
b = 2;
c = 3;
但你想,而不是根据你的问题到底是什么
but what you want instead according to the end of your question is
x = [1, 2, 3];
如果你定义 X
如上可以将参考 x的单个元素
像
If you define x
as above you can the refer to the individual elements of x
like
>> x(1), x(2), x(3)
ans =
1
ans =
2
ans =
3
现在你有1定义两全其美。您可以参考 A
, B
和 C
使用×(1)
, X(2)
, X(3)
来代替,而你只需要定义 X
一旦与 X = [1,2,3];
Now you have the best of both worlds with 1 definition. You can refer to a
, b
and c
using x(1)
, x(2)
, x(3)
instead and you've only had to define x
once with x = [1, 2, 3];
.
这篇关于在MATLAB中同时定义多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!