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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:32:32+00:00 2026-05-12T08:32:32+00:00

In Matlab when one tries to access an element of a matrix that doesn’t

  • 0

In Matlab when one tries to access an element of a matrix that doesn’t exist usually an error is raised:

>> month(0)
??? Subscript indices must either be real positive integers or logicals.

I was wondering whether there is a function that allows supplying default value in such cases. E.g.,:

>> get_def(month(0), NaN)
ans =
   NaN

P.S. I can workaround this particular case of subscript (0), but I was wondering about more generic way of doing this.

  • 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-12T08:32:32+00:00Added an answer on May 12, 2026 at 8:32 am

    There is no built-in MATLAB function to do what you want. You could use a try-catch block:

    >> try a = month(0); catch a = nan; end
    >> a
    
    a =
    
       NaN
    

    However, the best option would probably be to error-check the index first, throwing an error or setting a variable to a default value if it is out of range.

    If you really want to try using an index that may be 0, you could write your own get_def function. Here’s one way to do it:

    function value = get_def(vector,index,defaultValue)
      try
        value = vector(index);
      catch
        value = defaultValue;
      end
    end
    

    You would then use this function in the following way:

    >> month = 1:12;
    >> get_def(month,0,nan)
    
    ans =
    
       NaN
    
    >> get_def(month,1,nan)
    
    ans =
    
         1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one of the things that has always bothered me about Matlab. I understand
In Matlab, by the function min(), I can only get one single minimum element
I am trying to plot a matrix where each element is in one out
I see in the MATLAB help ( matlab -h ) that I can use
I'm porting some MATLAB functions to Scilab . The cool thing is that there
I have a 30000x14000 sparse matrix in MATLAB (version 7), which I need to
How does one solve the (non-trivial) solution Ax = 0 for x in MATLAB
I have several lists in Matlab that I want to write to the same
I'm developing an extension to MATLAB's PsychToolbox that allows for better control of the
How would one go plotting a plane in matlab or matplotlib from a normal

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.