Arrange string based on 1 or 0 value
2 次查看(过去 30 天)
显示 更早的评论
Hi. I have question regarding to cell string and matrix. I have matrix F and cell string D. I want to create another cell string Y.
F =
0 0 0 0
1 0 0 1
0 1 1 1
1 1 1 1
D =
'A'
'B'
'C'
'D'
Therefore, I want to create Y with output like this.
Y =
'-'
'A, D'
'B, C, D'
'A, B, C, D'
The steps are:
-1- recognize column positions of value 1 for row 1 in F.
-2- the column positions are equal to row positions of D. So, string in that row positions will input to row 1 of Y.
-3- do the same step for next row in F and D. If no value 1 for that row, that row position of Y will be empty, and maybe can be replace with sign - as to indicate no string available from D.
0 个评论
采纳的回答
Stephen23
2017-7-4
编辑:Stephen23
2017-7-4
[col,row] = find(F');
fun = @(v){sprintf(', %s',D{v})};
out = accumarray(row,col,[],fun);
out = cellfun(@(s)s(3:end),out,'uni',0)
out(cellfun('isempty',out)) = {'-'};
giving:
>> out{:}
ans = -
ans = A, D
ans = B, C, D
ans = A, B, C, D
更多回答(1 个)
KSSV
2017-7-4
F =[0 0 0 0
1 0 0 1
0 1 1 1
1 1 1 1] ;
D = {'A' 'B' 'C' 'D'} ;
F = logical(F) ;
for i = 1:4
iwant = D(F(i,:))
end
2 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!