String selection from Matrix data

2 Ansichten (letzte 30 Tage)
JB
JB am 10 Okt. 2017
Kommentiert: JB am 10 Okt. 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;
]

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 10 Okt. 2017
Bearbeitet: Andrei Bobrov am 10 Okt. 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);
  1 Kommentar
JB
JB am 10 Okt. 2017
Great. Thanks a lot Andrei Bobrov

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by