Matrix operations that preserve floating point precision

3 vues (au cours des 30 derniers jours)
Tolulope
Tolulope le 31 Août 2012
When concatenating a matrix from the row element of a cell array, I find that the floating points are always converted to integers. How do avoid this?
E.g.
Q = {[1;2;3],[4.000;5.000;6.000],[7e+04;8e+03;9e+02],[0.1234;0.34;0.4]};
P = [Q{1}];
for i=2:4
P = cat(2,P,Q{i});
end

Réponse acceptée

Sean de Wolski
Sean de Wolski le 31 Août 2012
Modifié(e) : Sean de Wolski le 31 Août 2012
The values will be converted to the most restrictive class so preconvert the integers to double:
Q = cellfun(@double,Q,'UniformOutput',0);
Also rather than the for-loop you can use the comma-separated list expansion of the cell array:
P = horzcat(Q{:});
Of course if you would like the best of both worlds (floats and integers) and would like to have them in a non-cell array (and you have the Statistics Toolbox) then you could use a dataset
Q = {uint8([1;2;3]),uint32([4.000;5.000;6.000]),[7e+04;8e+03;9e+02],[0.1234;0.34;0.4]};
D = dataset(Q{:})
  1 commentaire
Tolulope
Tolulope le 31 Août 2012
Yep I have got the Stat Toolkit, so the dataset function works a treat. Thanks

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by