View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Merge sorted arrays

4.5 | 2 ratings Rate this file 5 Downloads (last 30 days) File Size: 8.37 KB File ID: #28930 Version: 1.3

Merge sorted arrays


Bruno Luong (view profile)


03 Oct 2010 (Updated )

Given two already sorted arrays, the function combines them in an unique sorted array

| Watch this File

File Information

Using this function avoids to call Matlab SORT(), which does unnecessary work since the two inputs is already sorted.

Mex implementation for speed.
'rows' option supported


This file inspired Choose K.

MATLAB release MATLAB 7.11 (R2010b)
Other requirements Mex installed
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
02 Nov 2013 Bruno Luong

Bruno Luong (view profile)

Hi Paul, you are right this is a BUG. Submission including correction is pending.

Comment only
01 Nov 2013 Paul

Paul (view profile)

Hi Bruno, thanks for a useful tool. It comes in handy for efficiently summing sparse tensors together. On my system there appears to be a bug when one of the two arrays is empty (see example case below). Is this a compilation problem on my system or a bug in the algorithm? Thanks!

>> [C idx]=mergesa([],[1 1 1; 3 1 1],'rows')

C =

1 1 1
3 1 1

idx =


19 Jan 2012 Jan Simon

Jan Simon (view profile)

@Tal: C++ comments are accepted in the C99 style. Only C89 does not consider them, but this standard is 23 years old now.

Comment only
28 Mar 2011 Tal Darom

Tal Darom (view profile)

it works.
note that the .c files have c++ comments, resulting in errors when compiled with gcc.

file names should be changed to .cpp or use a c style comments.

04 Oct 2010 1.1

Optionally return the splitting locations of the inputs

31 Oct 2010 1.2

Minor speed improvement

04 Nov 2013 1.3

Fix bug of merge rows with empty array

Contact us