How can I assign a number to a letter combination in matlab?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jonathan Schipper
le 23 Sep 2021
Réponse apportée : Voss
le 18 Déc 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 commentaire
Image Analyst
le 23 Sep 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.
Réponse acceptée
Voss
le 18 Déc 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);
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Numeric Types dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!