フィルターのクリア

Covert a string to an array of individual characters.

1 回表示 (過去 30 日間)
Dimitrios Anagnostou
Dimitrios Anagnostou 2023 年 3 月 28 日
編集済み: Dyuman Joshi 2023 年 3 月 28 日
I apologize if my question is trivial.
I have the following datasets.The letters in each dataset represent student names enrolled in each course :
Course1=['A','B','C','E','F','G','I','P','Q'];
Course2=['B','E','F','H','K','Q','R','S','T','U','V','Z'];
Course3=['C','E','G','H','J','K','O','Q','Z'];
So for example
Course1orCourse2 = union(Course1,Course2)
Course1orCourse2 = 'ABCEFGHIKPQRSTUVZ'
Course1andCourse2 = intersect(Course1,Course2)
Course1andCourse2 = 'BEFQ'
How can I get an output in the form ['A', 'B', 'C', 'E', 'F', 'G', 'H', 'I', 'K', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'Z'] and ['B', 'E', 'F', 'G']? Thank you very much?
  1 件のコメント
Stephen23
Stephen23 2023 年 3 月 28 日
編集済み: Stephen23 2023 年 3 月 28 日
Note that square brackets are a concatentation operator (not a list operator like in some other langauges), so your examples are just complicated ways of writing character vectors. For example, this code:
Course1 = ['A','B','C','E','F','G','I','P','Q'];
is just a longer way of writing this equivalent character vector:
Course1 = 'ABCEFGIPQ';
The same applies to your requested outputs, e.g. this:
['A', 'B', 'C', 'E', 'F', 'G', 'H', 'I', 'K', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'Z']
is exactly equivalent to this simple character vector:
'ABCEFGHIKPQRSTUVZ'
And that is already what UNION and INTERSECT are giving you: character vectors which consist of lots of individual characters. Understanding what square brackets actually do, and what character vectors are, is very important when using MATLAB:

サインインしてコメントする。

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 3 月 28 日
編集済み: Dyuman Joshi 2023 年 3 月 28 日
If you need every alphabet presented individually, you will have to define them as strings.
Either manually define each input with double inverted commas
Course1=["A","B","C","E","F","G","I","P","Q"]; %similarly for Course2 and Course3
or use compose on the existing data -
Course1=['A','B','C','E','F','G','I','P','Q'];
Course2=['B','E','F','H','K','Q','R','S','T','U','V','Z'];
Course3=['C','E','G','H','J','K','O','Q','Z'];
Course1=compose("%s",Course1')';
Course2=compose("%s",Course2')';
Course3=compose("%s",Course3')'
Course3 = 1×9 string array
"C" "E" "G" "H" "J" "K" "O" "Q" "Z"
Course1orCourse2 = union(Course1,Course2)
Course1orCourse2 = 1×17 string array
"A" "B" "C" "E" "F" "G" "H" "I" "K" "P" "Q" "R" "S" "T" "U" "V" "Z"
Course1andCourse2 = intersect(Course1,Course2)
Course1andCourse2 = 1×4 string array
"B" "E" "F" "Q"

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by