From: Juliette Salexa on
Hello,
My program gives me an output like this:

1.000000000000000
0.485400238849356
0.046127510002059
0.564148944841658
0.311156957578463 + 0.000000000000000i
0.199273442026241 + 0.000000000000000i
0.199273442026241 + 2.155352552523552i

In the last value the imaginary part is necessary, but in the 2nd and 3rd last values, storing the real number as a complex number seems like a waste of memory and is suboptimal for practical reasons.

Is there a way where I could set matlab to store complex numbers as real numbers if the imaginary part is smaller than some threshold ?

I'd like to see if there's an easy method to do this globally rather than inserting IF statements into my program.

Cheers!
From: Sean on
"Juliette Salexa" <juliette.physicist(a)gmail.com> wrote in message <hqpmr7$ipd$1(a)fred.mathworks.com>...
> Hello,
> My program gives me an output like this:
>
> 1.000000000000000
> 0.485400238849356
> 0.046127510002059
> 0.564148944841658
> 0.311156957578463 + 0.000000000000000i
> 0.199273442026241 + 0.000000000000000i
> 0.199273442026241 + 2.155352552523552i
>
> In the last value the imaginary part is necessary, but in the 2nd and 3rd last values, storing the real number as a complex number seems like a waste of memory and is suboptimal for practical reasons.
>
> Is there a way where I could set matlab to store complex numbers as real numbers if the imaginary part is smaller than some threshold ?
>
> I'd like to see if there's an easy method to do this globally rather than inserting IF statements into my program.

a(imag(a)<2) = real(a(imag(a)<2))
From: Mark Shore on
"Juliette Salexa" <juliette.physicist(a)gmail.com> wrote in message <hqpmr7$ipd$1(a)fred.mathworks.com>...
> Hello,
> My program gives me an output like this:
>
> 1.000000000000000
> 0.485400238849356
> 0.046127510002059
> 0.564148944841658
> 0.311156957578463 + 0.000000000000000i
> 0.199273442026241 + 0.000000000000000i
> 0.199273442026241 + 2.155352552523552i
>
> In the last value the imaginary part is necessary, but in the 2nd and 3rd last values, storing the real number as a complex number seems like a waste of memory and is suboptimal for practical reasons.
>
> Is there a way where I could set matlab to store complex numbers as real numbers if the imaginary part is smaller than some threshold ?
>
> I'd like to see if there's an easy method to do this globally rather than inserting IF statements into my program.
>
> Cheers!

See Sean's example and bear in mind if these values are returned in a mixed array then the reals are still stored in complex format (but with an imaginary component of 0 and isreal return of true).
From: James Tursa on
"Juliette Salexa" <juliette.physicist(a)gmail.com> wrote in message <hqpmr7$ipd$1(a)fred.mathworks.com>...
> Hello,
> My program gives me an output like this:
>
> 1.000000000000000
> 0.485400238849356
> 0.046127510002059
> 0.564148944841658
> 0.311156957578463 + 0.000000000000000i
> 0.199273442026241 + 0.000000000000000i
> 0.199273442026241 + 2.155352552523552i
>
> In the last value the imaginary part is necessary, but in the 2nd and 3rd last values, storing the real number as a complex number seems like a waste of memory and is suboptimal for practical reasons.

In general, you will not be able to save any memory. Suppose you had a 1MB real array X. In that case MATLAB only stores the real part, there is no memory allocated for the imaginary part. Now suppose you change only *one* element in that 1MB array to have a non-zero imaginary part, e.g. X(1) = X(1) + 2i. All of a sudden MATLAB will have to create an additional 1MB imaginary part for X, even though the vast majority of this imaginary part is 0. There is no way around this given the way MATLAB stores data for the variables. Yes, it seems like a waste, but that is the way it is. But you have this penalty in other languages also (e.g., Fortran and C), so don't be too disappointed. In your above example the first four elements have blanks for the imaginary part when printing the output. Don't be fooled by this into thinking that there is no storage for this. In fact, there is storage
allocated for imaginary parts to *all* of the elements printed, even if some of them print blanks for the imaginary parts.

> Is there a way where I could set matlab to store complex numbers as real numbers if the imaginary part is smaller than some threshold ?

tol = 1e-10;
Y = abs(imag(X))<tol;
X(Y) = real(X(Y));

Be aware, however, that this simply sets the imaginary parts of the elements near zero to exactly zero. It will *not* save any memory. As long as you have at least one non-zero imaginary part in the array, the entire imaginary part of the array will be allocated.

James Tursa
From: Cris Luengo on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hqpq5l$pka$1(a)fred.mathworks.com>...
> tol = 1e-10;
> Y = abs(imag(X))<tol;
> X(Y) = real(X(Y));

You could also compare the magnitudes of the imaginary and real parts:

Y = abs(real(X)) > 1e6 * abs(imag(X));
X(Y) = real(X(Y));