How can I assign a number to a letter combination in matlab?

2 次查看(过去 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
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
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 个)

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by