MATLAB: How to select element in cell array, based on non-zero entries in another vector

MATLABnonzeroselection

Let's say I have two arrays:
a = [1 0 0 1];
b = {'ada', 'bob', 'carl', 'dana'};
where the first is a numerical array of zeros and ones, and the second is a cell array of character arrays. How can I achieve the following result:
{'ada','dana'}
where the resulting cell array only contains the elements based on the one values in a?

Best Answer

Just use basic logical indexing:
>> a = [1,0,0,1];
>> b = {'ada', 'bob', 'carl', 'dana'};
>> c = b(a==1)
c =
'ada' 'dana'