필터 지우기
필터 지우기

Convert cell of strings to numbers

조회 수: 3 (최근 30일)
Óscar
Óscar 2015년 2월 25일
댓글: Óscar 2015년 2월 26일
Hallo, On the one hand I have a cell of strings (with around 400 elements) and on the other hand in a loop I have another element (different each time) so I have to find the position of that element at the big array each time. Which is really slow.... so my intention is to convert the cell to numbers (the other string as well) and then just find in the array of number one number.
Is that possible?
Thanks in advance!
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2015년 2월 25일
편집: Azzi Abdelmalek 2015년 2월 25일
Can you post a short example? and post the code you are using
Óscar
Óscar 2015년 2월 25일
편집: Guillaume 2015년 2월 25일
Thanks, yes, this is my actual code:
aux=indexDictionary(ismember(dictionary,value));
where value is the string I want to seek in the dictionary which is something like:
dictionary ={'HOUSE';'CAT';'DOG';'WARDROBE';'BED';'MATTRESS';'SHEETS';'DESK';'LIGHT';....;};
And indexDictionary is just an array with these values: [1,2,3,4,5,6,7,..] so I can get the index of the word as easy as possible.
Then that line is inside a loop, so value is different each time. When I run the code with the Profiler tool, it executes that line around 200000 times which consumes more than 300s so I want to speed it up.
I don't really know how the function 'ismember' works, but I guess is faster for numbers than for strings. That's why I'd like to change the dictionary to a cell of int.
thanks

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

채택된 답변

Guillaume
Guillaume 2015년 2월 25일
Neither the ismember nor the indexDictionary indirection is necessary:
aux = find(strcmp(dictionary, value), 1);
In all likelyhood ismember dispatches to strcmp when given strings, so bypass the middleman which you don't need in your case.
  댓글 수: 1
Óscar
Óscar 2015년 2월 26일
Thank, This is the fastest way, with this line instead of the previous one the elapsed time is reduced from 300 to 28s.

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

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 2월 25일
str = {'a' 'b' 'c' 'd' 'e' 'f'}
w='c'
out=find(ismember(str,w))

Sara
Sara 2015년 2월 25일
If your cell array includes only numbers, you can use cell2mat

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by