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

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?

 Accepted Answer

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

1 Comment

Great! yes sir, First I tried in a similar way but stuck somewhere. I have deleted my answer.

Sign in to comment.

More Answers (0)

Products

Release

R2016a

Asked:

on 23 Aug 2018

Edited:

on 23 Aug 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!