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

2 Ansichten (letzte 30 Tage)
Jonathan Schipper
Jonathan Schipper am 23 Sep. 2021
Beantwortet: Voss am 18 Dez. 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 Kommentar
Image Analyst
Image Analyst am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 18 Dez. 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);

Weitere Antworten (0)

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by