equivalent of "a(b==1,:) = 3" for cellarray

Hello, I am looking for a search of cellarrays that works in the same way as:
a(b==1,:) = 3.
so ideally it would be
a(b=='*name*',:) = 3.
I have a cellarray with a list of participant names and I want to replace the names with numbers. I cannot search for absolute values, as names vary throughout different tests, e.g.Thomas1, Thomas2....
b = {'a' 'P1a'; 'b' 'P1b'; 'c' 'P1a'; 'd' 'P2asdf'; 'e' 'P2sd'; 'f' 'P3hm' };
and the result i want to get is:
a = [1; 1; 1; 2; 2; 3];

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 6일

1 개 추천

b= {'a' 'P1a'; 'b' 'P1b'; 'c' 'P1a'; 'd' 'P2asdf'; 'e' 'P2sd'; 'f' 'P3hm' }
a=str2double(regexp(b(:,2),'\d+','match','once'))

댓글 수: 1

Hanne Stenzel
Hanne Stenzel 2016년 7월 6일
Thanks for the quick answer. My names didn't contain any numbers so I used: a = regexprep(Results.(test).W(:,5),'(\w*)Andy(\w*)','1');
In this way, I have to loop over every possible name.

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

Stephen23
Stephen23 2016년 7월 6일
편집: Stephen23 2016년 7월 6일

0 개 추천

"I have to loop over every possible name."
No looping is required, because regexprep also accepts cell arrays as it inputs:
>> C = {'anna1';'bob2';'chaz1';'anna2';'X';'Y'}
C =
'anna1'
'bob2'
'chaz1'
'anna2'
'X'
'Y'
>> name = {'anna','bob','chaz'};
>> rgx = strcat('\w*',name,'\w*');
>> rpl = arrayfun(@num2str,1:numel(name),'uni',0);
>> regexprep(C,rgx,rpl)
ans =
'1'
'2'
'3'
'1'
'X'
'Y'

댓글 수: 3

Hanne Stenzel
Hanne Stenzel 2016년 7월 6일
Ah, great, thanks! Then my last question: one participant entered his full name with space: 'Teo Georg' when in the name-cellarray he appears as 'Teo'. How can I get rid of the second name after the space?
Stephen23
Stephen23 2016년 7월 7일
편집: Stephen23 2016년 7월 7일
>> C = {'anna1';'bob2';'chaz1';'anna2';'Teo Georg';'X';'Y'};
>> name = {'anna','bob','chaz','Teo'};
>> rgx = strcat('\w*',name,' ?\w*');
>> rpl = arrayfun(@num2str,1:numel(name),'uni',0);
>> regexprep(C,rgx,rpl,'ignorecase')
ans =
'1'
'2'
'3'
'1'
'4'
'X'
'Y'
Note that regular expressions are very pernickety things: they require careful design to make them work properly. They are not intelligent: they will only match exactly what they have been told to match. This means that you are responsible for knowing exactly what name patterns need to be matched, how these patterns are to be recognized, and then how to encode this in a regular expression.
I can't know if the regular expression above will work for all of your names, because I don't have all of your names, and I don't know what they could contain: space characters, leading or trailing numbers, capitals, lower case, etc, etc... all of these make a difference to regular expressions, but without knowing all of the permitted name styles and string formatting then I cannot say exactly what will work for your strings. The best I can do is to give you a solution that works for the examples that you have given us. Please ask here if you have any questions about regular expressions, or try my FEX submission for experimenting with regular expressions:
Hanne Stenzel
Hanne Stenzel 2016년 7월 7일
Hi Stephen, that tool is great. Thanks!

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

카테고리

태그

질문:

2016년 7월 6일

댓글:

2016년 7월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by