Hello everyone,
I want to replace text with numbers. 1=s, 3=r, 4=b . I tried 2 methods. Cant figure why niether of the codes wont work . What am I doing wrong?
% Method 1 / not working returns "Index exceeds the number of array elements (3)." error
mymat= [ 4 4 4
3 4 4
1 4 4
4 3 4
3 3 3
1 3 4
4 1 4]
t = string(["b", "r", "s"])
comMat = t(mymat)
%Method 2 / returns a NaN matrix and that's not what I want.
mymat= [ 4 4 4
3 4 4
1 4 4
4 3 4
3 3 3
1 3 4
4 1 4]
mymat(mymat==4)= "b"
mymat(mymat==3)= "r"
mymat(mymat==1)= "s"
mymat
thank you for your time in advance.

 採用された回答

Adam Danz
Adam Danz 2020 年 12 月 29 日
編集済み: Adam Danz 2020 年 12 月 31 日

1 投票

Here's a more flexible alternative.
mymat= [ 4 4 4
3 4 4
1 4 4
4 3 4
3 3 3
1 3 4
4 1 4];
t = ["s", "r", "b"];
v = unique(mymat(:)); % or maybe you want unique(mymat(:),'stable')
B = string(categorical(mymat,v,t))
B = 7×3 string array
"b" "b" "b" "r" "b" "b" "s" "b" "b" "b" "r" "b" "r" "r" "r" "s" "r" "b" "b" "s" "b"
Also works for any values
mymat(mymat==4) = 10;
mymat(mymat==1) = -42.5
mymat = 7×3
10.0000 10.0000 10.0000 3.0000 10.0000 10.0000 -42.5000 10.0000 10.0000 10.0000 3.0000 10.0000 3.0000 3.0000 3.0000 -42.5000 3.0000 10.0000 10.0000 -42.5000 10.0000
B = string(categorical(mymat, unique(mymat(:)), ["s", "r", "b"]))
B = 7×3 string array
"b" "b" "b" "r" "b" "b" "s" "b" "b" "b" "r" "b" "r" "r" "r" "s" "r" "b" "b" "s" "b"
Another alternative
B= discretize(mymat, [unique(mymat(:));inf], ["s", "r", "b"])
B = 7×3 string array
"b" "b" "b" "r" "b" "b" "s" "b" "b" "b" "r" "b" "r" "r" "r" "s" "r" "b" "b" "s" "b"

1 件のコメント

klb
klb 2021 年 1 月 1 日
Works! Thank you Adam for the solutions and your time.

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2020 年 12 月 29 日

0 投票

Well here's one way. Using a simple for loop
mymat= [ 4 4 4
3 4 4
1 4 4
4 3 4
3 3 3
1 3 4
4 1 4]
t = string(["b", "", "r", "s"])
[rows, columns] = size(mymat)
for col = 1 : columns
for row = 1 : rows
output(row, col) = t(mymat(row, col));
end
end
output % Show in command window.
I'm sure other people will show you other ways.
Jose
Jose 2020 年 12 月 31 日
編集済み: Jose 2020 年 12 月 31 日

0 投票

The easiest is to replace the numbers by ASCII codes corresponding to your letters and then convert to char. You can index each character as a normal matrix i.e. comMAT(1,1)
I have edited the answer to make it generic
comMAT=nan(size(mymat));
comMAT(mymat==4)=98;
comMAT(mymat==3)=114;
comMAT(mymat==1)=115;
comMAT=char(comMAT);

2 件のコメント

Stephen23
Stephen23 2020 年 12 月 31 日
編集済み: Stephen23 2020 年 12 月 31 日
Nice and simple, but has the disadvantage that it requires hard-coding each value separately, it could be looped to avoid that.
Important: this works because the output array is distinct from the input array. Note that the char conversion can be applied on the first line.
Jose
Jose 2020 年 12 月 31 日
編集済み: Jose 2020 年 12 月 31 日
I was assuming the objective was replacing single digit int by alphanumerical. That piece of code does that in a simple way. It can be very easily generalized if the output matrix comMAT is initialized to NaN and the transformation is done there. For a lot of values it will require a for loop that in that case is not very elegant solution, I agree.

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

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

質問済み:

klb
2020 年 12 月 29 日

コメント済み:

klb
2021 年 1 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by