Efficiently convert cell to double

2 次查看(过去 30 天)
John
John 2012-5-22
Hi, I have a cell array of strings ("position") and need to convert the first cell column into a vector of doubles ("pointX"). The following loop works as intended, but takes a very long time. Is there a more efficient way to accomplish the same goal? Thank you.
parfor j = 1:length(position)
pointX(j,1) = str2num(cell2mat(position{j,1}(1)));
end
  6 个评论
Oleg Komarov
Oleg Komarov 2012-5-23
You cannot do that.
You can try to change the problem at the core, e.g. by importing a file in a different way or by changing the routine that generates the cell, or you have to manipulate the cell array.
Jan
Jan 2012-5-24
@John, reading the textual description is still complictaed. It would reduce the chance of misunderstanding, if you post the Matlab code to create the test data and the wanted result, e.g.
position = {{'21', '34.5', '-17'}, {'324', '1e-19', '15'}}
result = [21, 324]
3 contributors tried to guess as good as they can, but there is a large chance, that we have wasted half an hour, because we did not understand the problem completely.

请先登录,再进行评论。

回答(3 个)

Jan
Jan 2012-5-22
[EDITED: THIS DOES NOT MEET YOUR DEMANDS] Sorry, due to a missing example, I've overseen the fact, that you need the first column only. Before I guess to much around, could you please add some example values by editing the original question?
str2double is slow. sscanf is fast:
c = cell(1, 10000); % Create test data
for i = 1:numel(c)
c{i} = sprintf('%d', floor(rand*1000));
end
tic;
v1 = str2double(c);
toc
tic;
S = sprintf('%s*', c{:});
v2 = sscanf(S, '%d*');
toc
Elapsed time is 0.411949 seconds. (Matlab 2009a/64 Win7.)
Elapsed time is 0.013777 seconds.
But sprintf cannot pre-allocate sufficiently, such that a dedicated MEX-function for concatenating cell elements beat this, see FEX: CStr2String:
tic;
S = CStr2String(c, '*');
v2 = sscanf(S, '%d*');
toc
Elapsed time is 0.005835 seconds.
71 times faster. To my deep surprise calling SSCANF inside the MEX directly is not faster, although it avoids the expensive allocation of the large string, see e.g. FEX: String to double. Therefore I'd rely either on the pure Matlab sscanf(sprint()) or use CStr2String in addition.
  4 个评论
madhan ravi
madhan ravi 2019-3-19
@dzid: read about sscanf() in the documentation

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2012-5-22
str2double() will be faster than str2num()
Also, could probably skip the parfor loop altogether and just call str2double() on the cell array of strings:
str2double({'3';'45'})

Oleg Komarov
Oleg Komarov 2012-5-23
To address the OP comments:
Suppose you have this fake input (6000 by 1 cell array where each cell is a 1 by 3 cellstring):
position = repmat({{'23','43','120'}},6000,1);
tic
% Manipulate into 6000 by 3
position = cat(1,position{:,1});
% Use str2double
position = str2double(position);
toc
Elapsed time is 0.754681 seconds.
If you want to achieve results faster, consider Jan's suggestions.
  6 个评论
Jan
Jan 2012-5-24
I'd dare to call it "significant" also.
Daniel Shub
Daniel Shub 2012-5-24
With Oleg's pause on 64-bit Linux with R2011a I get:
0.6394 6.1356

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by