フィルターのクリア

How to assign specific value for character

1 回表示 (過去 30 日間)
wesleynotwise
wesleynotwise 2017 年 5 月 21 日
コメント済み: Walter Roberson 2017 年 5 月 22 日
I have a cell array of character vectors, let say
A = {'r' 'b' 'g'; 'g' 'r' 'b'; 'b' 'r' 'g'}
how do I change this character to a specific value, eg: r = 0.5, b = 2, g = -3?

回答 (2 件)

Jos (10584)
Jos (10584) 2017 年 5 月 21 日
Here is an easy way:
A = {'r' 'b' 'g'; 'g' 'r' 'b'; 'b' 'r' 'g'}
RGBval = [0.5 2 -3]
[~,loc] = ismember(A,{'r','g','b'})
B = RGBval(loc)
  1 件のコメント
wesleynotwise
wesleynotwise 2017 年 5 月 21 日
編集済み: wesleynotwise 2017 年 5 月 21 日
Thanks. I am thinking to use the following command, but of course it is incorrect because the output is categorical array. Any idea how to correct it?
B = categorical(A,{'r' 'g' 'b'},{'0.5' '2' '-3'})

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


Walter Roberson
Walter Roberson 2017 年 5 月 21 日
RGBval('rgb') = [0.5 2 -3];
A = {'r' 'b' 'g'; 'g' 'r' 'b'; 'b' 'r' 'g'};
B = cellfun(@(C) RGBval(C), A);
  2 件のコメント
wesleynotwise
wesleynotwise 2017 年 5 月 22 日
Sorry. Can you explain what is
RGBval('rgb') = [0.5 2 -3]
It generates a 1x114 double output?
Walter Roberson
Walter Roberson 2017 年 5 月 22 日
RGBval('rgb') is the same as RGBval(['r', 'g', 'b']) which is the same as RGBval([double('r'), double('g'), double('b')] which is the same as RGBval([114, 103, 98]). Characters, except for char(0), can be used as indices for arrays, and a vector of characters is transformed using double() to become a vector of indices.
There is no requirement that you use the minimum-memory data structure for operations if you have enough available memory.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by