Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical indexing of cell array in MATLAB

Tags:

matlab

I want to do

myCellArray = myCcellArray{indices} 

where indices is just 0s and 1s, with same number of elements as the number of rows in myCellArray, but it doesn't work. What should I do?

like image 228
Trup Avatar asked Aug 09 '11 21:08

Trup


2 Answers

You need to use parenthesis instead of curly braces to do the indexing.

>> arr = cell(2,2);
>> arr{1,1} = magic(4);
>> arr{1,2} = 'Hello';
>> arr{2,1} = 42;
>> arr{2,2} = pi;
>> arr

arr = 

    [4x4 double]    'Hello' 
    [        42]    [3.1416]

>> idx = logical(zeros(2,2));
>> idx(1,1) = true;
>> idx(2,2) = true;
>> arr(idx)

ans = 

    [4x4 double]
    [    3.1416]
like image 84
Praetorian Avatar answered Nov 15 '22 05:11

Praetorian


If you want to slice a cell-array, use parentheses. Example:

%# random cellarray of strings, and a logical indices vector
myCcellArray = cellstr(num2str((1:10)','value %02d'));   %'
indices = rand(size(myCcellArray)) > 0.5;

%# slicing
myCellArray = myCcellArray(indices)
like image 44
Amro Avatar answered Nov 15 '22 05:11

Amro