replace first elements from 1 col to numbers

Hi, I have 26x1 cell matrix
s= {'O363'
'O321'
'null'
'O800'
'O411'
'O342'
'O820'
'O244'
'O13'
'O300'
'O623'
'O601'
'Z311'
'Z372'
'O811'
'Q738'
'O142'
'E039'
'B951'
'O64'
'G40'
'O11'
'E669'
'E059'
'I519'
'D65'
'O800'
'O411'
}
I want to replace first chars with numbers as:
s1 = [10363
10321
0
10800
10411
10342
10820
10244
1013
10300
10363
10601
11311
11372
10811
12738
10142
13039
14951
1064
1540
1011
13669
13059
16519
1765
10800
10411]

댓글 수: 7

Stephen23
Stephen23 2016년 2월 17일
편집: Stephen23 2016년 2월 17일
What is rule for encoding the characters? It seems that 'O' represents 10, and 'Z' represents 11, but what about the other characters: where is this encoding defined?
bero
bero 2016년 2월 17일
you can coded as you want, I think it is better from 10...
Stephen23
Stephen23 2016년 2월 17일
I do not understand what you mean. Do you want the character codes to be randomly allocated to some numbers? If so, what is the set or range of values, and for what characters?
If you want help then you actually need to explain what you you need.
Ok, the first element in the col is always char and this char may repeat so I need to assign a specific number to every char as:
B->10
D->11
E->12
F->13
G->14
H->15
I->16
J->17
O->18
P->19
Q->20
Z->21
This is the only chars that i need
So I know that 10800 is O800 and 10601 is O601...
I do not know if i explain well...
Stephen23
Stephen23 2016년 2월 17일
편집: Stephen23 2016년 2월 17일
That is much better, now we know something. How are these characters stored? Is it stored in a variable, such as in a cell array of strings?
it is better to get the first chars element from the columns as:
s1=[
O
N
O
O
O
O
O
O
O
O
O
Z
Z
O
Q
O
E
B
O
G
O
E
E
I
D
O
O]
Then unique (s1) then assign numbers to unique chars then replace chars with number in (s). The final data type is cell array.
bero
bero 2016년 2월 18일
Done Thanks...

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

 채택된 답변

Stephen23
Stephen23 2016년 2월 17일
편집: Stephen23 2016년 2월 17일

1 개 추천

chr = 'BDEFGHIJOPQZ';
num = 10+(0:numel(chr)-1);
org = strcat('^',[{'null'};num2cell(chr(:))]);
rep = [{'0'};cellstr(num2str(num(:)))];
out = regexprep(s,org,rep);
Gives strings in a cell array:
18363
18321
0
18800
18411
18342
18820
... more here
18800
18411
Note that this encoding follows the one that you gave in your comment, not the one in your original question.

댓글 수: 4

bero
bero 2016년 2월 17일
Good, can I use unique for the first elements of the columns to get the chr automatically??
Stephen23
Stephen23 2016년 2월 17일
편집: Stephen23 2016년 2월 17일
Sure, just replace the first line (defining chr) with these two lines:
tmp = regexp(s,'^[A-Z]','once','match')
chr = unique([tmp{:}]);
and the rest will work just fine.
bero
bero 2016년 2월 17일
편집: bero 2016년 2월 17일
Great, can I make the out double?? can I use str2double?? It seems there is an error... thanks
Stephen23
Stephen23 2016년 2월 17일
편집: Stephen23 2016년 2월 17일
If you want numeric values from the strings, then try using str2double:
str2double(out)
If that does not work then you actually need to give us more information than "It seems there is an error.", because we cannot read your computer screen and we cannot read your mind. If you tell us the complete error message and show the code you are using then we can help you.

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

추가 답변 (1개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2016년 2월 17일

댓글:

2016년 2월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by