Convert character matrix to numeric matrix

조회 수: 10 (최근 30일)
Raed Alahmadi
Raed Alahmadi 2019년 1월 12일
댓글: Raed Alahmadi 2019년 1월 14일
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
Raed Alahmadi
Raed Alahmadi 2019년 1월 12일
all element in my matrix have the same length so you can exclude that possibility
Image Analyst
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.).

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 1월 12일
If you really need a 2x2 array then one straightforward way would be to use a cell array:
>> A = {'AB','AC';'AD','AE'}
A =
'AB' 'AC'
'AD' 'AE'
>> B = cellfun(@double,A,'uni',0)
B =
[65,66] [65,67]
[65,68] [65,69]
>> C = cellfun(@char,B,'uni',0)
C =
'AB' 'AC'
'AD' 'AE'
  댓글 수: 1
Raed Alahmadi
Raed Alahmadi 2019년 1월 14일
Thanks for your help I used your method it is good but I face some other problems in my code so I change it slightly. Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
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)
  댓글 수: 1
Raed Alahmadi
Raed Alahmadi 2019년 1월 14일
Thanks for your help I used other method to do it but I think I will need also your code in my code. thanks a lot

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by