Speed up the conversion of array of cells in numbers
Show older comments
I have a problem regarding the use of a for loop. I would like to avoid this loop in order to have a faster execution of my code.
I have a vector of cells called str_cell. The length of the vector is NUM_LINES = 300. Each cell contains a string of 11 numbers in a string format, which are delimited by a TAB.
In the for loop I convert the cells in vectors of numbers and I pass them in an array, called data, which has been previously initialized.
Here the code
for i = 1:NUM_LINES
numbers = str2double(strread(str_cell{i},'%s','delimiter','\t'));
data(i, :) = numbers(:);
end
When NUM_LINES is 300, my computer takes ~290ms to do that. I tried to avoid the for loop, but the command str2double works with a single string so that I had to pass cell by cell. Moreover I cannot change the way to get the files, in order to by-pass the problem. Do you have suggestions about avoiding this for loop?
Thanks in advance
2 Comments
Sean de Wolski
on 15 Feb 2013
How did you end up with this cell array? Did it come from a file? If it did, you could probably read it in as numbers.
Guido Maria Re
on 16 Feb 2013
Accepted Answer
More Answers (2)
Azzi Abdelmalek
on 15 Feb 2013
a={'1 2 3','4 5 6','7 8 10'}
out=str2num(char(a))
Guido Maria Re
on 16 Feb 2013
0 votes
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!