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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:16:30+00:00 2026-06-11T13:16:30+00:00

Suppose that f is a function of one parameter whose output is an n

  • 0

Suppose that f is a function of one parameter whose output is an n-dimensional (m1 × m2… × mn) array, and that B is a vector of length k whose elements are all valid arguments for f.

I am looking for a convenient, and more importantly, “shape-agnostic”, MATLAB expression (or recipe) for producing the (n+1)-dimensional (m1 × m2 ×…× mn × k) array obtained by “stacking” the k n-dimensional arrays f(b), where the parameter b ranges over B.

To do this in numpy, I would use an expression like this one:

C = concatenate([f(b)[..., None] for b in B], -1)

In case it’s of any use, I’ll unpack this numpy expression below (see APPENDIX), but the feature of it that I want to emphasize now is that it is entirely agnostic about the shapes/sizes of f(b) and B. For the types of applications I have in mind, the ability to write such “shape-agnostic” code is of utmost importance. (I stress this point because much MATLAB code I come across for doing this sort of manipulation is decidedly not “shape-agnostic”, and I don’t know how to make it so.)


APPENDIX

In general, if A is a numpy array, then the expression A[..., None] can be thought as “reshaping” A so that it gets one extra, trivial, dimension. Thus, if f(b) is an n-dimensional (m1 × m2… × mn) array, then, f(b)[..., None] is the corresponding (n+1)-dimensional (m1 × m2 ×…× mn × 1) array. (The reason for adding this trivial dimension will become clear below.)

With this clarification out of the way, the meaning of the first argument to concatenate, namely:

[f(b)[..., None] for b in B]

is not too hard to decipher. It is a standard Python “list comprehension”, and it evaluates to the sequence of the k (n+1)-dimensional (m1 × m2 ×…× mn × 1) arrays f(b)[..., None], as the parameter b ranges over the vector B.

The second argument to concatenate is the “axis” along which the concatenation is to be performed, expressed as the index of the corresponding dimension of the arrays to be concatenated. In this context, the index -1 plays the same role as the end keyword does in MATLAB. Therefore, the expression

concatenate([f(b)[..., None] for b in B], -1)

says “concatenate the arrays f(b)[..., None] along their last dimension”. It is in order to provide this “last dimension” to concatenate over that it becomes necessary to reshape the f(b) arrays (with, e.g., f(b)[..., None]).

  • 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-11T13:16:31+00:00Added an answer on June 11, 2026 at 1:16 pm

    One way of doing that is:

     % input:
     f=@(x) x*ones(2,2)
     b=1:3;
     %%%%
     X=arrayfun(f,b,'UniformOutput',0);
     X=cat(ndims(X{1})+1,X{:});
    

    Maybe there are more elegant solutions?

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

Sidebar

Related Questions

I have the following function that is supposed to trigger anytime one of the
Suppose that my Haskell function is given an input, which is supposed to be
Suppose that you have a function f: List a -> a such that f
Suppose that in the main I have a function a : a(c); and c
Suppose I have a function that returns a closure: sub generator { my $resource
Suppose I'm writing a function that takes a list of integers and returns only
I'm trying to write a Powershell function that takes an array argument. I want
As a toy example, suppose that we have a function called 'my_func' (the code
There is a following function that is supposed to make a comparison between 2
Let's say I want to implement a function that is supposed to process an

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.