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?

 采纳的回答

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

1 个评论

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

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by