How to eliminate or exclude complix numbers from an array solution?

425 views (last 30 days)
Hello everyone,
Is there any way to eliminate the complex numbers (real and imaginary parts) from a array solution. Or can I exclude the complex elements so the solution will just include the real numbers. For example, in the next array I just want the real elements ( the first four elements. Thanks for any suggestion.
sol =
2.7081096834177947291918098879734
2.4539692532957056897462100829847
1.1392825187840759291489282043638
-4.0494620185638170836265057842329
0.88295725432536163128309307530013 - 0.1308999971886537594046901453934*i
0.1308999971886537594046901453934*i + 0.88295725432536163128309307530013
0.1528510148755374525528985048959*i - 2.0089069727922412635133142708447
- 0.1528510148755374525528985048959*i - 2.0089069727922412635133142708447
  2 Comments
Walter Roberson
Walter Roberson on 8 Sep 2020
fs = double(subs(func, {l,m}, {lvalue, mvalue}));
mask = imag(fs) == 0;
[maxvalue, maxidx] = max(fs(mask));
selected_l = lvalue(mask);
selected_m = mvalue(mask);
best_l = selected_l(maxidx);
best_m = selected_m(maxidx);

Sign in to comment.

Accepted Answer

Matt J
Matt J on 14 Feb 2016
sol = sol(sol==real(sol));

More Answers (1)

Jose Perez-Sanabria
Jose Perez-Sanabria on 7 Sep 2020
x=x(imag(y)==0);
y=y(imag(y)==0);
  2 Comments
Walter Roberson
Walter Roberson on 17 Jun 2021
You fix your objective function so that it never returns complex values, or you set up constraints so that you avoid the area in which it would return complex values. or you use some other kind of multi-objective minimizer that supports complex values.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!