How can I convert every floating value in cell to integer?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
siddhesh rane
le 3 Mar 2014
Commenté : per isakson
le 3 Mar 2014
I have a cell with matrices of different sizes in it .I want to convert every floating value in cell to integer. I tried int16 function but apparently it can only be used for arrays..is there any function for cells?
0 commentaires
Réponse acceptée
per isakson
le 3 Mar 2014
Modifié(e) : per isakson
le 3 Mar 2014
Not with a standard function. However, it is possible with a small script
>> cellfun( @(num) int16(num), {[1,2,3],[4,5]; [6], [7,8] }, 'uni', false )
ans =
[1x3 int16] [1x2 int16]
[ 6] [1x2 int16]
3 commentaires
per isakson
le 3 Mar 2014
Modifié(e) : per isakson
le 3 Mar 2014
It should work if raw_data is a flat cell array, which only contains numerical data.
per isakson
le 3 Mar 2014
It will take more than a few lines of code to convert all numerical data in any cell array to integer.
A few case can be solved by using the function, flatten. Try
raw = { [1,2,3], [4,5]; {[6],[7,8,9]}, [0] };
cellfun( @(num) int16(num), raw, 'uni', false )
returns
Error using int16
Conversion to int16 from cell is not possible.
Error in @(num)int16(num)
and
cellfun( @(num) int16(num), flatten(raw), 'uni', false )
returns
ans =
[1x3 int16] [6] [1x3 int16] [1x2 int16] [0]
which asks for reshape
Plus de réponses (1)
Jos (10584)
le 3 Mar 2014
Two step approach for mixed cell arrays
C = {(1:10)/3, 'hello',[pi exp(1)]}
tf = cellfun(@isnumeric,C) % which cell are numeric?
C(tf) = cellfun (@(x) int16(x), C(tf),'un',0) % only convert those
0 commentaires
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!