Quick conversion of a large (and with mixed data types) cell array into a numerical array
Mostra commenti meno recenti
Hi, I have a 800'000 x 20 cell array and I would like to convert it into a 800'000 x 20 double precision array.
My cell array has the following features:
- it is a mix of data types, i.e. some columns are character vectors (which represent numbers) and some columns are numbers.
- within a column of character vectors, the number of characters is different among the rows.
Here, an example/part of my cell array:
a =
10×3 cell array
{'1170790,889999999897555'} {'1009,697999999999979'} {[ 1]}
{'1170260,709000000031665'} {'869,886999999999944' } {[ 2]}
{'1181959,743999999947846'} {'504,125999999999976' } {[ 3]}
{'1174019,564000000013039'} {'674,142000000000053' } {[ 4]}
{'1173729,793000000063330'} {'689,635999999999967' } {[ 5]}
{'1181555,257999999914318'} {'509,391999999999996' } {[ 6]}
{'1180374,280000000027940'} {'538,126999999999953' } {[ 7]}
{'1180124,550999999977648'} {'568,719000000000051' } {[ 8]}
{'1179305,570999999996275'} {'626,528999999999996' } {[ 9]}
{'1177132,448000000091270'} {'705,443999999999960' } {[10]}
Any suggestion to quickly/efficiently convert this (mixed data type) cell array into a numerical array, possibly without using str2double() ?
Just for information: among my attempts for conversion, I found that str2double() is very slow and
>> cell2mat(a(:,2))
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
and
>> str2num(char(a(:,3)))
Error using char
Cell elements must be character arrays.
2 Commenti
Stephen23
il 17 Feb 2020
Your character vectors appear to use decimal commas, which are treated as thousands separators by str2double:
>> str2double('12,345')
ans =
12345
and will result in separate numbers if you use str2num:
>> str2num('12,345')
ans =
12 345
I doubt that either of those are very useful to you.
Sim
il 17 Feb 2020
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Data Type Conversion in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!