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 8847483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:12:01+00:00 2026-06-14T12:12:01+00:00

In Matlab, I am using ndgrid to create a 6D matrix. Here is the

  • 0

In Matlab, I am using “ndgrid” to create a 6D matrix. Here is the code:

for i=1:3
    dd{i}=[0 0 0 0 0 0 0 1 1 1 1 1];
    ss{i}=[0 0 0 0 0 0 1 1 1 1 1 1];
end 
[D1 D2 D3 S1 S2 S3] = ndgrid(dd{1},dd{2},dd{3},ss{1},ss{2},ss{3});
out = D1.*S1.*D2.*S2.*D3.*S3;   

The problem that I am having is that although I have plenty of memory to store one or two 6-D matrices, I do not have enough memory to store all 6 matrices:

[D1 D2 D3 S1 S2 S3] 

As you can see, the matrix D1, D2… and “out” are sparse in nature, but the “sparse” functionality in Matlab does not work on multidimensional arrays. I have searched for other “grid” functionality in Matlab, but I can not find a “grid” function that helps me avoid the intermediary step of calculating D1, D2 etc.

In general, I want to allow dd{1} to be different than dd{2}. Also, this 6-D case I posted doesn’t take up too much memory, but the 8-D case does, which is where I am running into an issue.

Any help making more efficient use of memory in this situation is much appreciated.

  • 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-14T12:12:02+00:00Added an answer on June 14, 2026 at 12:12 pm

    Here’s a case where a loop will almost certainly be faster (and will fit!), since you’re memory-bound. Just use 6 (or 8) nested for-loops, and drop in each element of out, one at a time. Use the nested loop indices to reference directly into your source vectors, and just be sure you match the correct index with the correct vector. This will limit you only to the maximum size of your output array.

    Editing with a new idea:

    Ooooh, I just thought of bsxfun, which would do exactly the right thing, except it only accepts two arguments. It essentially replicates as necessary across any singleton dimensions. Pairwise-nesting bsxfun calls would be pointless, as the intermediate results would be back to full size. But it is a great way to reduce your dimension by half, which for a power term is a big deal:

    [D1 D2 D3] = ndgrid(dd{1},dd{2},dd{3});
    [S1 S2 S3] = ndgrid(ss{1},ss{2},ss{3});
    out = bsxfun(@times, D1.*D2.*D3, reshape(S1.*S2.*S3, [1 1 1 12 12 12]));
    

    Obviously the 12s have to be replaced by dynamic sizes. Now, instead of D*N^D elements, you only need D*N^(D/2). You won’t have a memory problem until you’re back up to 14 or 16 dimensions.

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

Sidebar

Related Questions

Using MATLAB, I have this code: value = input('>> Enter a value: '); and
I know that you can create a file browser in Matlab using uigetfile. However,
I have a problem that is driving me nuts. Matlab sees only some of
I want to send a Matrix to Matlab using engine.h in c++ code. The
I want to create a classification tree in Matlab using classregtree. However, a lot
I have been using Matlab 2011b and contourf/contourfm to plot 2D data on a
I'm generating a MATLAB GUI using GUIDE, but I want to create fields when
I'm trying to figure out the difference between histograms made on Matlab (using the
Using MatLab, I know how to create a line segment connecting two points using
Using Matlab, I have to transform the intensity of an image using this given

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.