How can I assign a number to a letter combination in matlab?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi!
I have trouble assigning a number to a letter combination in a table in Matlab, for instance x=0, f=10, s=1.
the table looks something like this
x x f s
x f x s
x fs x ss
I want this to convert to a table like this:
0 0 10 1
0 10 x 1
0 11 0 2
Can someone help me with this problem?
댓글 수: 1
Image Analyst
2021년 9월 23일
There are many ways that could be a table, like a 1x1 table, a 1x4 table, a 4x1 table, etc. So please attach your actual table in a .mat file or give code to contruct it from those letters.
채택된 답변
Voss
2021년 12월 18일
Let's say the initial table was a 2-D cell array of chars called t and you want to convert it to a matrix of doubles called y.
t = { ...
'x' 'x' 'f' 's'; ...
'x' 'f' 'x' 's'; ...
'x' 'fs' 'x' 'ss'};
display(t);
lookup = { ...
'x' 0; ...
's' 1; ...
'f' 10};
y = zeros(size(x));
for i = 1:size(lookup,1)
y = y+lookup{i,2}*cellfun(@(x)nnz(x==lookup{i,1}),t);
end
display(y);
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!