Friday, September 18, 2009

MATLAB/Octave Utils: submat, grouped

Totally forgot to share these two MATLAB/Octave functions I wrote some time ago:
submat: accessing a sub-matrix of an unknown multi-dimensional matrix
grouped: creating record like data structures from arrays grouped by an index cell

Actually, I still wonder if or why this functionality is not part of the core library or even the 'language syntax '? Anyhow, I couldn't find it so I wrote the two functions myself. (please tell me if you know about a predefined equivalent or a better solution)


1. submat ( submat.m )

In Matlab everything is a (potential) N-dimensional matrix of elements such as doubles, cells or structs. Sub-indexing is easy, unless you try to program in a generic way, e.g. with high-order functions. The problem is that traditional sub-indexing requires you to know the element type (cell or not) and dimensions of the matrix. So I came up with a function submat, which basically does sub-indexing in the highest dimension of an unknown type:

A = [ 1, 2, 3; 4, 5, 6 ];
submat(A,2:3) % same as A(:,2:3)
-> [ 2, 3; 5, 6 ]

B = { 'Hello', 'submat'; 'Goodbye', '(:)' };
submat(B',1) % same as B'{:,1}
-> { 'Hello', 'submat' }



2. grouped ( grouped.m )

The other thing I missed was a simple way to group data arrays to structs. Of course this can easily be done within a for loop, but using the group function eliminates the code repetition. Also, it can be be used in two ways:

First, it allows to simply group equal indices together.

grouped( [ 1, 2, 1, 3, 1, 2, 3] )
-> { [ 1, 3, 5 ], [ 2, 6 ], [ 4, 7 ] }

So far nothing special, but its main purpose is to create a custom struct array.

%artists = { 'Beatles', 'Bob Dylan' };
%records

record_artist_ids = [1, 1, 2, 2, 1];
% or the result of group(record_artist_ids)
record_artist_ids = { [1, 2, 5], [3, 4] };
title = { 'White Album', 'Rubber Soul', 'Blood on the Tracks', 'Blonde on Blonde', 'Revolver' };
year = [ 1968, 1965, 1975, 1966, 1966 ];

artist_records = grouped ( record_artist_ids, 'title', title, 'year', year)

-> artist_records(1).title = { 'White Album', 'Rubber Soul', 'Revolver' }
artist_records(1).year = [ 1968, 1965, 1966 ]
artist_records(2).title = { 'White Album', 'Rubber Soul', 'Revolver' }
artist_records(2).year = [ 1975, 1966 ]