sub2ind_mex

A very fast sub2ind routine in mex ( C )

You are now following this Submission

This function operates in the same manner as the 'sub2ind' method. However, this method is quite faster. It is very similar to the sub2ind.m code.
Example :
>> a = [ 50 50 50 ];
>> b = [ 1 30 49 ];
>> x = sub2ind(a,b,b,b)
x =

1 73980 122449

>> xx = sub2ind_mex(a,b,b,b)

xx =

1 73980 122449

I would like to acknowledge "Marco" from the following thread. He began this bit of code a while ago and posted it here. I have simply taken it and improved its performance a bit. Thank you Marco, I do hope you don't mind what I have done here.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/262036

Update :::
I had unfortunately made a pretty big error in the code. The first while loop did not have sound logic as it was counting backwards. This would work for symmetric arrays only.
My apologies everyone!

Cite As

Christopher Harris (2026). sub2ind_mex (https://www.mathworks.com/matlabcentral/fileexchange/49165-sub2ind_mex), MATLAB Central File Exchange. Retrieved .

General Information

MATLAB Release Compatibility

  • Compatible with any release

Platform Compatibility

  • Windows
  • macOS
  • Linux
Version Published Release Notes Action
1.1.0.0

Bug fix. While loop removed.

1.0.0.0