round function on complex numbers

16 views (last 30 days)
Shashank K Holla
Shashank K Holla on 30 Apr 2019
Edited: Stephen23 on 30 Apr 2019
So I have entered a complex array like this
a = [1,1,1,1; 1 -j -1 -1; 1 -1 1 -1; 1 j -1 -j]
And get output as
a =
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i
1.0000 + 0.0000i 0.0000 - 1.0000i -1.0000 + 0.0000i -1.0000 + 0.0000i
1.0000 + 0.0000i -1.0000 + 0.0000i 1.0000 + 0.0000i -1.0000 + 0.0000i
1.0000 + 0.0000i 0.0000 + 1.0000i -1.0000 + 0.0000i 0.0000 - 1.0000i
which is fine.
But I want to round this array to show complex numbers as integers. Eg 1 + 0i, 1+ i etc. How do I achieve this?
round() function doesnt seem to do anything as
>> round(a)
ans =
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i
1.0000 + 0.0000i 0.0000 - 1.0000i -1.0000 + 0.0000i -1.0000 + 0.0000i
1.0000 + 0.0000i -1.0000 + 0.0000i 1.0000 + 0.0000i -1.0000 + 0.0000i
1.0000 + 0.0000i 0.0000 + 1.0000i -1.0000 + 0.0000i 0.0000 - 1.0000i

Answers (1)

Stephen23
Stephen23 on 30 Apr 2019
Edited: Stephen23 on 30 Apr 2019
This has nothing to do with rounding. Either change the format, e.g.:
>> format short g
>> a = [1,1,1,1; 1 -j -1 -1; 1 -1 1 -1; 1 j -1 -j]
a =
1 1 1 1
1 0 - 1i -1 -1
1 -1 1 -1
1 0 + 1i -1 0 - 1i
Or write your own display code using fprintf, e.g.:
>> fmt = [repmat(' %d%+di',1,size(a,2)),'\n'];
>> fprintf(fmt,a.')
1+1i 1+1i 1+0i -1-1i
1-1i 1-1i 1+0i -1+0i

Tags

Products

Community Treasure Hunt

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

Start Hunting!