Concatenating Arrays with Different Dimensions
2 views (last 30 days)
Show older comments
Jordan Means
on 24 Sep 2018
Reopened: Walter Roberson
on 22 Dec 2018
I'm trying to concatenate these arrays with one line of code. How do I go about doing that?
The final array was supposed to be 3 x 4. I tried using the cat command, but because the dimensions are different, I had trouble doing it.
x = rand(1,3)
x = 10*x
x = round(x)
y = rand(1,3)
y = 10*y
y = round(y)
z = rand(1,4)
z = 10*z
z = round(z)
q = rand(2,1)
q = 10*q
q = round(q)
0 Comments
Accepted Answer
John D'Errico
on 24 Sep 2018
You can want to do all sorts of things that are not possible to do.
But you cannot create a double precision array that has a varying amount of elements in each row or column. Arrays are RECTANGULAR things. So an array that has 3 elements in one row, and 4 elements in another will fail to work. It is invalid syntax to try to construct such a thing. Wanting is insufficient, unless of course, you will also decide to rewrite MATLAB from scratch. (Have fun with that.)
You COULD decide to pad zeros, or perhaps NaNs to some of those vectors as necessary, so they are all the same lengths. Or, you could use a cell array to store the vectors.
0 Comments
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!