Complex Numbers in Matrices

16 views (last 30 days)
MM
MM on 29 Nov 2020
Answered: Walter Roberson on 29 Nov 2020
My program loop generates 60
N(i,1) = [complex #1; complex #2; complex #3; complex #4],
Here, all the complex numbers are different. Each N has two complex numbers with positive imaginary part and two with negative imaginary part.
I need to isolate two complex numbers with positive imaginary part. And store this complex number in
Matrix1= First accepted Complex Number with positive imaginary part
Matrix2= Second accepted Complex Number with positive imaginary part.
Such that both Matrix 1 and 2, should have 60 complex number each obtained from N.

Answers (1)

Walter Roberson
Walter Roberson on 29 Nov 2020
format short
%create some data that has the required characteristics
N = complex(rand(4,60), rand(4,60));
for K = 1 : 60; c = randperm(4,2); N(c,K) = conj(N(c,K)); end
%now process
Matrix1 = N(sub2ind(size(N), sum(cumprod(imag(N) < 0))+1, 1:size(N,2)));
Matrix2 = N(sub2ind(size(N), sum(cumprod(imag(N) > 0))+1, 1:size(N,2)));
%and check.
disp(N(:,1:3))
0.2657 - 0.7314i 0.2445 - 0.5625i 0.4933 - 0.9174i 0.9369 - 0.1687i 0.9194 + 0.6287i 0.9663 + 0.5250i 0.7501 + 0.2729i 0.6338 - 0.9384i 0.8267 + 0.3453i 0.2002 + 0.2955i 0.5082 + 0.0311i 0.8804 - 0.3498i
disp(Matrix1(1:3))
0.7501 + 0.2729i 0.9194 + 0.6287i 0.9663 + 0.5250i
disp(Matrix2(1:3))
0.2657 - 0.7314i 0.2445 - 0.5625i 0.4933 - 0.9174i
%the below is not relevant to the code you wanted. I happened to notice a display bug
%so I am using the below as part of a report to Mathworks.
N(:,1:4)
ans = 4×4
0.2657 - 0.7314i 0.2445 - 0.5625i 0.4933 - 0.9174i 0.7601 - 0.6164i 0.9369 - 0.1687i 0.9194 + 0.6287i 0.9663 + 0.5250i 0.8809 - 0.8984i 0.7501 + 0.2729i 0.6338 - 0.9384i 0.8267 + 0.3453i 0.1348 + 0.8749i 0.2002 + 0.2955i 0.5082 + 0.0311i 0.8804 - 0.3498i 0.8497 + 0.7985i
N(:,1:5)
ans = 4×5
0.2657 - 0.7314i 0.2445 - 0.5625i 0.4933 - 0.9174i 0.7601 - 0.6164i 0.8942 - 0.4792i 0.9369 - 0.1687i 0.9194 + 0.6287i 0.9663 + 0.5250i 0.8809 - 0.8984i 0.9630 + 0.9265i 0.7501 + 0.2729i 0.6338 - 0.9384i 0.8267 + 0.3453i 0.1348 + 0.8749i 0.1853 + 0.2537i 0.2002 + 0.2955i 0.5082 + 0.0311i 0.8804 - 0.3498i 0.8497 + 0.7985i 0.7903 - 0.6989i
N(1:10)
ans = 1×10
0.2657 - 0.7314i 0.9369 - 0.1687i 0.7501 + 0.2729i 0.2002 + 0.2955i 0.2445 - 0.5625i 0.9194 + 0.6287i 0.6338 - 0.9384i 0.5082 + 0.0311i 0.4933 - 0.9174i 0.9663 + 0.5250i

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!