Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:28:33+00:00 2026-06-15T10:28:33+00:00

I have a 1024x3x3 matrix A and another of same dimensions B . I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:28:34+00:00Added an answer on June 15, 2026 at 10:28 am

    A one line solution to your problem is this:

    D = permute(cat(4, A, B), [1 4 2 3]);
    

    However, this needs some explaining. Here is an example to get us started:

    %# A 3-d pre-allocation example
    A = rand(3, 3, 3);
    B = rand(3, 3, 3);
    D = NaN(3, 3, 3, 2);
    D(:, :, :, 1) = A;
    D(:, :, :, 2) = B;
    

    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 cat function to solve the problem:

    %# The 3-d cat solution
    A = rand(3, 3, 3);
    B = rand(3, 3, 3);
    D = cat(4, A, B);
    

    The first argument of cat provides 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:

    %# Another 3-d pre-allocation example
    A = rand(3, 3, 3);
    B = rand(3, 3, 3);
    D = NaN(3, 2, 3, 3);
    D(:, 1, :, :) = A;
    D(:, 2, :, :) = B;
    

    But perhaps a better method that doesn’t involve explicit allocation is to use the trick with cat to create an extra dimension, and then use permute to re-arrange the dimensions into the order we want, eg:

    %# Another 3-d example with cat and permute
    A = rand(3, 3, 3);
    B = rand(3, 3, 3);
    D = cat(4, A, B);
    D = permute(D, [1 4 2 3]);
    

    Hope this helps. Cheers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have an app that can use tts to read text messages. It can also
Have such a problem, hope you'll help me.. Can't find anywhere. Here is the
Have deployed numerous report parts which reference the same view however one of them
Have finally got a responsive site working (of a fashion). What I want to
have been trying couple of hours now to make my iphone app universal. The
Have a look at one of my websites: moskah.com The problem is that it
Have a simple contact us XPage created. Have server side validation in place that
Have a really weird one here. The app has two targets, just to be
Have one Doubt In MVC Architecture we can able to pass data from Controller
Have a network location that shows paths in the 8.3 short format. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.