Show older comments
例えば変数A=["apple", "apple","banana", "orange"];
といった中に文字が入力されている変数があるとします。
これをapple=1,banana=2, orange=3だとしてA=[1,1,2,3]のように変換したいと考えています。
いくつか方法を試したのですが、上手くいかず困っています。
ご教授いただけると幸いです。
Accepted Answer
More Answers (4)
本質問の対象となるリリースはR2022a、下記回答はR2022bで導入された機能ですが、一応書いておきます。
fruits = ["apple","banana","orange"];
numbers = [1,2,3];
dic = dictionary(fruits,numbers)
A = ["apple","apple","banana","orange"];
dic(A) % arrayfunが要らない!
文字列一致判定で数値を代入する例を記述します。
A=["apple", "apple", "banana", "orange"];
Anum = zeros(1, length(A));
Anum(strcmp(A, "apple")) = 1;
Anum(strcmp(A, "banana")) = 2;
Anum(strcmp(A, "orange")) = 3;
disp(Anum);
mapObj = containers.Map(["apple","banana","orange"], [1,2,3])
A = ["apple","apple","banana","orange"];
arrayfun(@(x) mapObj(x),A)
type fruits.m % 添付のクラス定義ファイルを表示
A = ["apple","apple","banana","orange"];
Aenum = arrayfun(@(x) fruits.(x), A)
Anum = double(Aenum)
Categories
Find more on MATLAB 入門 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!