Matrix operations that preserve floating point precision

3 views (last 30 days)
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

Accepted Answer

Sean de Wolski
Sean de Wolski on 31 Aug 2012
Edited: Sean de Wolski on 31 Aug 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 Comment
Tolulope
Tolulope on 31 Aug 2012
Yep I have got the Stat Toolkit, so the dataset function works a treat. Thanks

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!