File Exchange

image thumbnail

sub2ind_mex

version 1.1.0.0 (930 Bytes) by Christopher Harris
A very fast sub2ind routine in mex ( C )

1 Download

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.
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 (2020). sub2ind_mex (https://www.mathworks.com/matlabcentral/fileexchange/49165-sub2ind_mex), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

I will be very happy for MEX version of ind2sub. Could you add this function, too.

To answer Royi,
Yes it does work.
The update on Jan 26 2015 was to correct an assumtion I had made. You see, I had been indexing from the end of the array back. This would be correct if the sub arrays were all the same....

All in all, the update was to simply count from the beginning of the array.
It should still have about 2X the speed of the matlab function.

Royi Avital

I didn't understand your comment. Does it work or not?

Updates

1.1.0.0

Bug fix. While loop removed.

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags