A very fast sub2ind routine in mex ( C )
Updated 26 Jan 2015

View License

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.

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 (2024). sub2ind_mex (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Matrices and Arrays in Help Center and MATLAB Answers
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Bug fix. While loop removed.