How to get matrix to read from top to bottom?

12 views (last 30 days)
Hi,
I'm currently working on problems in which I take the roots of a function and am returned with some real and complex numbers in 3 different columns for 300 rows. I then need to extract the real values in order from row 1 to 300 no matter what column they are in.
For the most part I have no issue as the real numbers would be in column 1 for about the first half of the rows, then column 3 for the rest of the rows. On one of the problems though the real values alternate between column 1 and column 3 multiple times from row 1 to 300. Because of this Matlab reads column 1 values from row 1 to ~100 or so then stay in column 1 for the remaining reals in before it reads the ones in column 3. Is there any way I can get Matlab to read whatever real is in each column before moving to the next row no matter what column it is in?

Answers (2)

KSSV
KSSV on 6 Dec 2017
If A is your 300X3 matrix...
Get real from column 2 using:
realcol2 = real(A(:,2))
Get real from column 3 using:
realcol3 = real(A(:,3))
  3 Comments
KSSV
KSSV on 6 Dec 2017
You can use isreal column wise....you will get logical indexing.
Guillaume
Guillaume on 6 Dec 2017
isreal is one of these confusing functions that tells you if the whole array is real rather than the individual elements.

Sign in to comment.


Guillaume
Guillaume on 6 Dec 2017
Because of this Matlab reads column 1 values from row 1 to ~100 or so then stay in column 1
Matlab does not do anything unless you tell it to. This reading is because that's what you told matlab to do. Unfortunately, you haven't shown us the code for that, so it's hard to tell what you mean by reads. The behaviour you describes looks like standard matlab linear indexing but nothing forces you to use linear indexing. I also suspect that you used find (as most beginners and even some non beginners do) even though it's not necessary:
transposedarray = yourmatrix';
realvaluesbyrow = tranposedarray(imag(transposedarray) == 0)
Should do what you want.

Community Treasure Hunt

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

Start Hunting!