Home > other >  Index of the number with the lowest real part, of a complex number
Index of the number with the lowest real part, of a complex number

Time:11-23

I have a problem with some complex numbers, which I rarely work with. My problem is that I have an array with made by
cos^(-1)(x) where x can be values smaller or larger than |1|, thereby getting some complex numbers, I need the index of the number with the lowest real part.

the data could look like

[0   0.37i, 0   0.18i,  0.2   0.0i, 0.3   0.0i, 0.4   0.0i]

so I need the index of 0.2 0.0i

what I have tried so far is

[val_min_x,idx_min_x]=min(real(x)>0))

since I need the smallest value of the real part larger then zero. But this wont work and I guess its because real(x>0) gives out true or false. And then taking the minimum of that just gives the index of the first zero.

Any suggestions to solve this without an if statement?

CodePudding user response:

First convert all 0s to 1s, so that min() will not find them (by addition of 1). Then keep everything else untouched (by addition of 0). A boolean trick will keep everything in one line for brevity,

Thanks @CG

% example input
A=[0   0.37i, 0   0.18i, 0.2   0.0i, 0.3   0.0i, 0.4   0.0i]
% leveraging maximum cos(x)<=1 and boolean true being cast to 1
[~, idx] = min(real(A) (real(A)==0))


A =

  Columns 1 through 3

   0.0000   0.3700i   0.0000   0.1800i   0.2000   0.0000i

  Columns 4 through 5

   0.3000   0.0000i   0.4000   0.0000i


idx =

     3
  • Related