converting categorical array to cell array

151 ビュー (過去 30 日間)
Boy
Boy 2016 年 4 月 8 日
編集済み: cui,xingxing 約11時間 前
Hi all, I am trying to convert a categorical array to a cell array so that I can use the strrep function, but i cant seem to find anyway to do it after scouring the web to the best of my abilities. My categorical column contains string that have the '_' character and i want to search the column and replace any string that has '_' with '-'. Is there any way I can do it without converting the datatype? Any help would be greatly appreciated. Thank you

採用された回答

Fangjun Jiang
Fangjun Jiang 2016 年 4 月 8 日
a=nominal({'R_','G_','B_'});
b=cellstr(a);
c=strrep(b,'_','-');
d=nominal(c)
d =
R- G- B-
  2 件のコメント
Boy
Boy 2016 年 4 月 8 日
移動済み: Fangjun Jiang 2024 年 1 月 8 日
Thank you for the quick response Fangjun. But i have a dataset where column 3 is nominal datatype. Can i change the entire column from nominal to cell array? Thank You
Salma Hassan
Salma Hassan 2017 年 12 月 24 日
移動済み: Fangjun Jiang 2024 年 1 月 8 日
C = cellstr(A) check this link https://www.mathworks.com/help/matlab/ref/cellstr.html

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

その他の回答 (1 件)

cui,xingxing
cui,xingxing 2024 年 1 月 6 日
編集済み: cui,xingxing 約11時間 前
@Fangjun Jiang update new workflow.
a=categorical({'R_','G_','B_'});
b=string(a);
c=replace(b,'_','-');
d=categorical(c)
d = 1×3 categorical array
R- G- B-
BTW, Convert categorical array to cell array in general way Example.
A = categorical(["A","B","C","D","hello","world"]) % categorical array
A = 1×6 categorical array
A B C D hello world
dst = num2cell(A) % cell array
dst = 1×6 cell array
{[A]} {[B]} {[C]} {[D]} {[hello]} {[world]}
if A is a complex arrays,also can use mat2cell function.
A = categorical(["A","B","Hello","world .";
"1","2","3","四"])
A = 2×4 categorical array
A B Hello world . 1 2 3 四
dst = mat2cell(A,[1,1],[2,1,1])
dst = 2×3 cell array
{[A B]} {[Hello]} {[world .]} {[1 2]} {[3 ]} {[四 ]}
-------------------------Off-topic interlude, 2024-------------------------------
I am currently looking for a job in the field of CV algorithm development, based in Shenzhen, Guangdong, China,or a remote support position. I would be very grateful if anyone is willing to offer me a job or make a recommendation. My preliminary resume can be found at: https://cuixing158.github.io/about/ . Thank you!
Email: cuixingxing150@gmail.com
  1 件のコメント
Fangjun Jiang
Fangjun Jiang 2024 年 1 月 8 日
Thank you for the update.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by