Convert character matrix to numeric matrix
显示 更早的评论
Hello
I have this matrix:
A=['AB' 'AC';'AD' 'AE'];
I want to make each element of this words to have unique value of number. After that I want a code to transfer the numbers to the origin words.
I used this code:
A=['AB' 'AC';'AD' 'AE'];
A = double(A)
A = char(A)
The problem is that the output has the size 2*4 while it should be as the input 2*2 this due to that matlab deals with the letters not the words so it make each letter in separate column.
Please help me.
Thanks
3 个评论
This line
A=['AB' 'AC';'AD' 'AE'];
is equivalent to:
A=['ABAC';'ADAE'];
so your input is not 2x2 at all, it is actually a 2x4 char array.
You could use a 2x2 cell array or a string array, but then you would have to consider the possibility that each string/char vector would have different length (or do you exclude that possibility?) and so each numeric vector would also have a different length, and so would not be able to be concatenated into one matrix.
Raed Alahmadi
2019-1-12
Image Analyst
2019-1-12
Your final A is a character array yet you say you want a numerical array. Please state EXACTLY what you'd like to see as the output for the example you've given, so we know whether it needs to be numbers (like 1, 2, etc.), ASCII values (like 65, 66, etc.), or characters (like 'A', 'B', etc.).
采纳的回答
更多回答(1 个)
Steven Lord
2019-1-12
If I understand your goal correctly, use the unique function.
sampleWords = {'apple', 'banana', 'cherry'};
n = numel(sampleWords);
data = sampleWords(randi(n, 1, 10))
[uniqueWords, generateUniqueWordsFromData, generateDataFromUniqueWords] = unique(data);
data2 = uniqueWords(generateDataFromUniqueWords)
isequal(data, data2)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!