String selection from Matrix data

2 ビュー (過去 30 日間)
JB
JB 2017 年 10 月 10 日
コメント済み: JB 2017 年 10 月 10 日
Hi guys. I need a little help from you Matlab experts.
I have a cell array (Names) with a list of 14 different names. Then I have a 15x3 matrix (Selection) with some random numbers from 0-14.
Now what I want to do is use each row in the "selection" matrix to make a list of names corresponding to the number in each cell. As an example the first row is [1 3 12] shall make thes list of names {Dave Smith Niko}. The third row is [9 2 0] and should result in the list {Chris Andy}.
It's ok to get the name list's out in a combined 15x3 cell array for instance from a loop.
PLEASE help me sort this out. Thanks
Names = {
'Dave'
'Andy'
'Smith'
'Eric'
'Donna'
'Lisa'
'Gawin'
'Bill'
'Chris'
'Crystal'
'Beth'
'Niko'
'Conny'
'Clara'
}
Selection = [
1 3 12;
3 6 8;
9 2 0;
4 2 1;
8 5 7;
3 2 0;
4 7 1;
4 9 12;
0 0 0;
0 0 0;
0 0 0;
0 0 0;
0 0 0;
0 0 0;
0 0 0;
]

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 10 月 10 日
編集済み: Andrei Bobrov 2017 年 10 月 10 日
Names(end+1) = {'none'};
Selection(Selection == 0) = numel(Names);
out = Names(Selection);
or
out = cellfun(@(x)reshape(Names(x(x~=0)),1,[]),num2cell(Selection,2),'un',0);
  1 件のコメント
JB
JB 2017 年 10 月 10 日
Great. Thanks a lot Andrei Bobrov

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by