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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:46:36+00:00 2026-06-02T04:46:36+00:00

I was trying to do sub2ind on single-precision variables that I came across the

  • 0

I was trying to do sub2ind on single-precision variables that I came across the following strange behavior. For example, when I try:

[a b] = ind2sub([50000 50000], sub2ind([50000 50000], single(1000), single(1000)))

I get:

a = 1001
b = 1000

Is this a bug or I am missing something? I know this can be because of an overflow somewhere in matlab’s code but it shouldn’t happen, right?

I get the same wrong behavior from 64-bit (glnxa64) R2012a, R2011a, R2010b, R2010a, but correct results from 32-bit (glnx86) R2010b.

  • 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-02T04:46:38+00:00Added an answer on June 2, 2026 at 4:46 am

    The reason this happens is the following.

    On line 35 of ind2sub.m is

    vi = rem(ndx-1, k(i)) + 1;   
    

    where ndx is the index that is passed in. Thus ndx is 49951000 via the call from sub2ind. Now, when you pass in a single precision value, it forces matlab to evaluate all math in single precision. Thus, compare the difference in what happens on l.35.

    K>> 49951000-1
    ans =
            49950999
    
    K>> single(49951000)-1
    ans =
            49951000
    

    This subtraction of a small number from a large number is the issue. So no, this isn’t a bug, it’s a limitation to the precision of single-precision floating point. This might help some.

    edit: As pointed out by Rasman, a nice way of showing this is with eps

    eps(single(49951000))=4
    

    so any value added or subtracted from single(49951000) that is on the range (-4,4) will result in 495100 being returned due to the accuracy of single-precision.

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

Sidebar

Related Questions

Trying to perform a single boolean NOT operation, it appears that under MS SQL
Trying to add rounded corners to images, that also have a border. Came up
Trying to implement the following behavior on an iPad. I have a map-centric application
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to figure out how to read in multiple variables through a file. for
Trying to create a simple document that gets the parentNode then applies a background
Trying to create an Oracle trigger that runs after a table is updated in
Trying to implement a MSMQ-backed WCF PubSub. I understand that net.msmq is one-way; however
Trying to construct a query such that I have multiple statement specifying joins, each
Trying to find some simple SQL Server PIVOT examples. Most of the examples that

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.