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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by