« Earlier6 items total Later »

Comma seperated lists

Matlab has some extremely nice notation for working with lists of items and indexing through lists of items. The simplest way to work with CSLs is to use cell arrays. Cells arrays are lists and you can build one as below.

a =  { 'cat' 'dog' 'cow' }


Now what can you do with this list? First of all you can index individual elements.

>> a{1}
ans =
   'cat'
>> a{2}
ans =
   'dog'


You can index a range of elements
>>a{1:2}
ans =
  'cat'
ans =
  'dog'

In the case what has been returned above is a comma seperated list, effectivly a tuple of answer, not a single object but a set of answers.

You can regroup the CSL using a number of concatenation techniques.

Using {}
>> { a{1:2} }
ans =
 'cat' 'dog'


Using []
>> { a{1:2} }
ans =
 'catdog'


Using () to provide function arguments
>> a = { 1 2 };
>> b = sum( a{1:2} )
b =
 3





Indexing speed of arrays with different index data types

Here is a simple test to prove that integers are
faster than singles or doubles for indexing matlab arrays.

%% test indexing
function untitled

     function test(type)
        % eg : int32(floor(rand(1,100)*100+1))
         k = eval(sprintf('%s(floor(rand(1,100)*100 + 1))', type));
         x = rand(1,100);
         tic
         for i = 1:1e5
             y = x(k);
         end
         toc
         fprintf('%s\n', type);
     end

     test('int32');
     test('uint32');
     test('single');
     test('double');

end

The results are
Elapsed time is 0.194030 seconds.
int32
Elapsed time is 0.195234 seconds.
uint32
Elapsed time is 0.413456 seconds.
single
Elapsed time is 0.804347 seconds.
double


Thus indexing with integers is definately faster.

Fold an array using a function handle

Usage
>> a = { 'cow' 'cat' 'dog' };
>> fold(a,@(str, item) [ str  ',' item  ] )

ans =

cow,cat,dog

or
>> a = { 'cow' 'cat' 'dog' };
>> fold(a,@(str, item) [ str  ',' item  ],'' )

ans =

,cow,cat,dog

Implementation
function init = fold(arr, fh, init)
    if nargin == 2
        start = 2;
        if iscell(arr)
            init=arr{1};
        else
           init = arr(1);
        end
    else
        start = 1;
    end
    for i = start:length(arr)
        if iscell(arr)
            init = fh(init, arr{i});
        else
            init = fh(init, arr(i));
        end
    end
end

Search an array of items using your own comparison function

Usage
>> a = { 'cow' 'cat' 'dog' };
>> find_in_array(a,@(x)x(1)=='c')

ans =

    'cow'    'cat'


Implementation
function out = find_in_array(arr, fh)
    if ~iscell(arr)
        out = [];
        for i=1:length(arr)
            if fh(arr(i))
                out = [ out arr(i) ];
            end
        end
    else
        out = {};
        for i=1:length(arr)
            if fh(arr{i})
                out = { out{:} arr{i} };
            end
        end
    end
end

Perform an operation on each item of an array and collect the results

>> a = { 'cow' 'cat' 'dog' };
>> collect(a, @(x)fliplr(x))

ans =

    'woc'    'tac'    'god'


Implementation
function out = collect(arr, fh)
    out = cell(size(arr));
    for i = 1:length(arr)
        if iscell(arr)
            out{i} = fh(arr{i});
        else
            out{i} = fh(arr(i));
        end
    end
end

Perform an operation on each item of an array

>> a = { 'cow' 'cat' 'dog' };
>> each(a, @(x)disp(fliplr(x)))
woc
tac
god
>>


Implementation for each.m
function each(arr, fh)
    for i=1:length(arr)
        if iscell(arr)
            fh(arr{i});
        else
            fh(arr(i));
        end
    end
end

« Earlier6 items total Later »




Sponsored by

Sole Central

Your one stop shop for Birkenstock and Crocs shoes and sandles.