Matrix operations that preserve floating point precision

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Sean de Wolski
Sean de Wolski il 31 Ago 2012
Modificato: Sean de Wolski il 31 Ago 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 Commento
Tolulope
Tolulope il 31 Ago 2012
Yep I have got the Stat Toolkit, so the dataset function works a treat. Thanks

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Dates and Time 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!

Translated by