String selection from Matrix data

1 view (last 30 days)
JB
JB on 10 Oct 2017
Commented: JB on 10 Oct 2017
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;
]

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 10 Oct 2017
Edited: Andrei Bobrov on 10 Oct 2017
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);

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!