Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define multiple variables at the same time in MATLAB?

I don't know if MATLAB can do this, and I want it purely for aesthetics in my code, but can MATLAB create two variables at the same time?

Example

x = cell(4,8);  
y = cell(4,8);

Is there a way to write the code something similar to:

x&y = cell(4,8);
like image 337
user379362 Avatar asked Mar 01 '11 17:03

user379362


People also ask

How do you create multiple variables at once?

You can assign multiple values to multiple variables by separating variables and values with commas , . You can assign to more than three variables. It is also possible to assign to different types. If there is one variable on the left side, it is assigned as a tuple.

How do you define multiple variables in one line?

When assigning multiple variables in a single line, different variable names are provided to the left of the assignment operator separated by a comma. The same goes for their respective values except they should be to the right of the assignment operator.

Can I declare multiple variables in single line?

Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values. If more than one variable is declared in a declaration, care must be taken that the type and initialized value of the variable are handled correctly.

Can we assign multiple values to multiple variables at a time?

We can assign values to multiple variables at once in a single statement in Swift. We need to wrap the variables inside a bracket and assign the values using the equal sign = . The values are also wrapped inside a bracket.


1 Answers

Use comma-separated lists to get multiple variables in the left hand side of an expression.

You can use deal() to put multiple assignments one line.

[x,y] = deal(cell(4,8), cell(4,8));

Call it with a single input and all the outputs get the same value.

[x,y] = deal( cell(4,8) );

>> [a,b,c] = deal( 42 )
a =
    42
b =
    42
c =
    42
like image 178
Andrew Janke Avatar answered Sep 18 '22 00:09

Andrew Janke