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

3 visualizaciones (últimos 30 días)
Jonathan Schipper
Jonathan Schipper el 23 de Sept. de 2021
Respondida: Voss el 18 de Dic. de 2021
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 comentario
Image Analyst
Image Analyst el 23 de Sept. de 2021
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.

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 18 de Dic. de 2021
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);

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by