I have a 1024x3x3 matrix A and another of same dimensions B. I want to make a matrix that is 1024x2x3x3 that is a combination of the two, can somebody help? My matlab skills suck.
I have a 1024x3x3 matrix A and another of same dimensions B . I
Share
A one line solution to your problem is this:
However, this needs some explaining. Here is an example to get us started:
The problem is conceptually much more straightforward if you begin by pre-allocating the output matrix you want, and then manually allocate the input matrices to the output matrix. However, once you’ve grasped this concept, you can use one call to the
catfunction to solve the problem:The first argument of
catprovides the dimension you want to concatenate along. By choosing a dimension that is one greater than the current maximum dimension of our matrix, we create a new dimension and concatenate along it.So, this solves the problem if what we want to do is add a new dimension at the end of our current set of dimensions. However, in the question, you state that you want the new dimension to appear as the second index. A simple extension of the pre-allocation example that accomodates this is:
But perhaps a better method that doesn’t involve explicit allocation is to use the trick with
catto create an extra dimension, and then usepermuteto re-arrange the dimensions into the order we want, eg:Hope this helps. Cheers.