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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:52:15+00:00 2026-05-31T11:52:15+00:00

In Matlab, I was trying to put anonymous functions in an array: >> a=[@(k)0.1/(k+1)

  • 0

In Matlab, I was trying to put anonymous functions in an array:

>> a=[@(k)0.1/(k+1)    @(k)0.1/(k+1)^0.501]
??? Error using ==> horzcat
Nonscalar arrays of function handles are not allowed; use cell arrays
instead.

So I wonder what kinds of elements are allowed in an array, and in a cell array?

For example, I know that in an array, the elements can be numerical or strings. What else?

  • 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-05-31T11:52:17+00:00Added an answer on May 31, 2026 at 11:52 am

    In short: Cell array is a heterogeneous container, regular array is homogeneous. This means that in a regular array all of the elements are of the same type, whereas in cell array, they can be different. You can read more about cell array here.

    Use cell array when:

    • You have different types in your array
    • You are not sure whether in the future you might extend it to another types
    • You are working with objects that have an inheritance pattern
    • You are working with an array of strings – almost in any occasion it is preferable to char(n,m)
    • You have a large array, and you often update a single element in a function – Due to Matlabs copy-on-write policy
    • You are working with function handles (as @Pursuit explained)

    Prefer regular array when:

    • All of the elements have the same type
    • You are updating the whole array in one shot – like mathematical operations.
    • You want to have type safety
    • You will not change the data type of the array in the future
    • You are working with mathematical matrices.
    • You are working with objects that have no inheritance

    More explanation about copy-on-write:

    When you pass an array to a function, a pointer/reference is passed.

    function foo(x)
         disp(x);
    end
    
    x= [1 2 3 4 5];
    foo(x); %No copy is done here! A pointer is passed.
    

    But when you change it (or a part of it), a copy is created.

    function foo(x)
        x(4) = x(4) + 1;
    end
    
    x= [1 2 3 4 5];
    foo(x); %x is being copied! At least twice memory amount is needed.
    

    In a cell array, only the cell is copied.

    function foo(x)
       x{4} = x{4} + 1;
    end
    
    x= {1 2 3 4 5}; %Only x{4} will be copied
    

    Thus, if you call a function that changes a single element on a large array, you are making a lot of copies – that makes it slower. But in a cell array, it is not the case.

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

Sidebar

Related Questions

I'm trying to plot the Dirac delta function in Matlab using plot , but
I am trying to sub-array in MATLAB with no luck. This is what I
I'm trying to translate the usage of find function in Matlab to C++ .
I'm trying to embed Python into a MATLAB mex function on OS X. I've
I'm trying to write some code to calculate a cumulative distribution function in matlab.
I'm trying to ouput an array of numbers as a string in MATLAB. I
I was trying to simulate MATLAB's NN functions before testing my own coded network.
I am trying to get this in MATLAB, however I am not successful !
I am trying to convert the Matlab function watershed into C# or C++. I
I am trying to speeden up my Matlab program by writing a few functions

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.