How can I split an array of float point numbers in two different arrays based on a dot (.) delimiter?
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Abdulatif Alabdulatif
il 15 Apr 2014
Modificato: Walter Roberson
il 15 Apr 2014
Hi all,
I try to spilt an array of float point numbers in two different arrays based on a dot (.) delimiter but it does not work. I do the following steps:
1- Convert an array of float point numbers to be as a string.
2- spilt a string based on a dot (.) delimiter.
This is not work and shows the following error:
Error using strsplit (line 80) First input must be a string.
Error in decompose (line 8) C = strsplit(str,'.');
Sorry if there is any spelling errors. I am beginner in Matlab.
Thanks in advance!
0 Commenti
Risposta accettata
Walter Roberson
il 15 Apr 2014
Modificato: Walter Roberson
il 15 Apr 2014
Aint = fix(A);
Adecimals = (abs(A) - abs(Aint)) * sign(A);
Or
T = rem(A, 1);
Adecimals = abs(T);
Aint = A - T;
If the numbers are certain to be non-negative then you can use
Aint = floor(A);
Adecimals = mod(A,1);
Note: if you are expecting that (for example) 3.14 would split into 3 and 14, then you have the problem that 3.14 is the same as 3.140 which would visually split into 3 and 140 instead. You would also have problems with the fact that 0.1 is not exactly representable in binary floating point, so 0.14 is actually represented internally as 0.14000000000000001332267629550187848508358001708984375
0 Commenti
Più risposte (1)
Chandrasekhar
il 15 Apr 2014
numbers can be converted into strings using 'num2str' command and they can be splitted using 'strtok' command
1 Commento
Walter Roberson
il 15 Apr 2014
num2str() uses a default format that might not be suitable -- though you can specify a format as the second parameter.
regexp() with the 'split' option can also be used to split strings.
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!