Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Concatenating matrix columns causes loss of precision

Subject: Concatenating matrix columns causes loss of precision

From: Rohit

Date: 23 Aug, 2013 05:41:07

Message: 1 of 5

Hi all,
I have a curious problem I'm hoping someone can help me with. 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 zeroed out.

Subject: Concatenating matrix columns causes loss of precision

From: James Tursa

Date: 23 Aug, 2013 06:45:12

Message: 2 of 5

"Rohit" wrote in message <kv6sli$fml$1@newscl01ah.mathworks.com>...
> Hi all,
> I have a curious problem I'm hoping someone can help me with. 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 zeroed out.

As long as the data types are the same, concatenation doesn't change the precision of anything. E.g., using your numbers:

>> format long
>> temp1 = [1334320224.86767;1334320225.03415;1334320225.20064]
temp1 =
  1.0e+009 *
   1.33432022486767
   1.33432022503415
   1.33432022520064
>> temp2 = [25.59989 -17.82167 31.19241;25.17558 -17.59459 30.71448;25.18788 -17.39987 30.61347]
temp2 =
  25.59989000000000 -17.82167000000000 31.19241000000000
  25.17558000000000 -17.59459000000000 30.71448000000000
  25.18788000000000 -17.39987000000000 30.61347000000000
>> temp = [temp1 temp2]
temp =
  1.0e+009 *
   1.33432022486767 0.00000002559989 -0.00000001782167 0.00000003119241
   1.33432022503415 0.00000002517558 -0.00000001759459 0.00000003071448
   1.33432022520064 0.00000002518788 -0.00000001739987 0.00000003061347
>> isequal(temp1,temp(:,1))
ans =
     1
>> isequal(temp2,temp(:,2:4))
ans =
     1

Everything internally is the same. I suggest you review what you did and you will discover you did something else besides just concatenation of two variables of the same datatype.

James Tursa

Subject: Concatenating matrix columns causes loss of precision

From: Rohit

Date: 23 Aug, 2013 07:15:07

Message: 3 of 5

Hi James,
Thanks for the reply. I missed mentioning a fact which I think is causing the problem. The example given is just 3 rows of a matrix having half a million rows. With these 3 rows it does work fine. Its only with the large matrix that I am facing the problem mentioned

Subject: Concatenating matrix columns causes loss of precision

From: James Tursa

Date: 23 Aug, 2013 07:43:21

Message: 4 of 5

"Rohit" wrote in message <kv725r$p17$1@newscl01ah.mathworks.com>...
> Hi James,
> Thanks for the reply. I missed mentioning a fact which I think is causing the problem. The example given is just 3 rows of a matrix having half a million rows. With these 3 rows it does work fine. Its only with the large matrix that I am facing the problem mentioned

3 or 3 million, the result should be the same. The data before & after should be identical as long as the data types are the same. There is no change in the data as a result of concatenation. I still think you need to look at your code and data harder.

James Tursa

Subject: Concatenating matrix columns causes loss of precision

From: Roger Stafford

Date: 24 Aug, 2013 04:03:05

Message: 5 of 5

"Rohit" wrote in message <kv6sli$fml$1@newscl01ah.mathworks.com>...
> I want the resulting matrix to have the original precision of temp1. How do I do this?
- - - - - - - - -
  It is important to realize that what you see in matlab's display is only a decimal representation of the number actually used by the computer but rounded off to a certain number of significant decimal digits depending on the rounding format. When you display an entire array of numbers of greatly differing sizes, the display generally uses the same number of significant digits for all elements. In your example you were originally displaying the first array to ten digits and the second one to seven. The display of the the concatenated array was to seven which makes it appear as though you have lost the last three digits of the first column, but this is untrue. As James has said, there will be no change in the numbers as stored internally to your machine.

  If you want to see the precise numbers as they are stored and used for computations, use "format hex" which represents the exact 64 binary digits that are used to represent a double precision floating point number, or the 32 for a single precision number. These hexadecimal numbers are not easy to interpret, since they are in the IEEE 754 format. To accomplish this see, for example, the pdf file at:

 http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us