smilingbuddha
smilingbuddha

Reputation: 14660

Stretching out an multidimensional array

Is there any built in MATLAB command that "stretches" out a multidimensional array into a linear array?

e.g [1,2;3,4] should be come [1,2,3,4]

Upvotes: 0

Views: 827

Answers (2)

user244795
user244795

Reputation: 708

You can also use the colon operator:

x = [1 2; 3 4];
y = x(:);

Upvotes: 3

eykanal
eykanal

Reputation: 27027

The reshape command can do this:

x = [1 2; 3 4];
y = reshape(x, 1, []);

The empty array [] indicates that MATLAB should calculate automatically how many elements should go in that direction (i.e., that you won't have to specify the number of elements in your array).

Upvotes: 1

Related Questions