Concatenating matrix in MATLAB results in loss of precision

I have a column matrix say temp1 with floating point numbers in the following format(displayed this way using format long g):

1334320224.86767
1334320225.03415
1334320225.20064

and another nx3 matrix (temp2) with values like so:

25.59989 -17.82167 31.19241
25.17558 -17.59459 30.71448
25.18788 -17.39987 30.61347

I concatenate the 2 matrices column wise, temp = [temp1 temp2]; The resulting matrix is:

1.334305e+09 24.40084 -17.98591 30.31327
1.334305e+09 24.23554 -17.68831 30.00396
1.334305e+09 25.31328 -17.61529 30.83927

I want the resulting matrix to have the original precision of temp1. How do I do this? I have already tried format long g. Writing to a file with dlmwrite and precision set to %.5f results in the fractional part of first column zeroed out.

Answers


First, format long g worked for me. I am using Matlab_R2013a on mac:

>> temp = [temp1 temp2]

temp =

          1334320224.86767                  25.59989                 -17.82167                  31.19241
          1334320225.03415                  25.17558                 -17.59459                  30.71448
          1334320225.20064                  25.18788                 -17.39987                  30.61347

But a simple solution: sprintf('%f ',temp), it will lose the matrix look-like formating, but you will be able to see as you want. The output:

>> sprintf('%f ',temp)

ans =

1334320224.867670 1334320225.034150 1334320225.200640 25.599890 25.175580 25.187880 -17.821670 -17.594590 -17.399870 31.192410 30.714480 30.613470 

If you reaaaally need to see as you pointed out you might want to do:

>> arrayfun(@(in) sprintf('%f',in),temp,'Uniform',0)

ans = 

    '1334320224.867670'    '25.599890'    '-17.821670'    '31.192410'
    '1334320225.034150'    '25.175580'    '-17.594590'    '30.714480'
    '1334320225.200640'    '25.187880'    '-17.399870'    '30.613470'

Need Your Help

appending new data to specific elements in lists in r

r list append rbind

Please correct me if my terminology is wrong because on this question Im not quite sure what Im dealing with regarding elements, objects, lists..I just know its not a data frame.

How to get the characters of a text-box after a newline in VB.Net

vb.net eol

Right now I am building a scripting language (EngineScript). Currently you can only type a single command/function (IE: sout "Hello world") but I want to expand it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.