当只有一个元素变得复杂时,为什么Matlab会创建虚部?

use*_*459 2 matlab complex-numbers

K>> asdfasdf=[1 1 1]

asdfasdf =
 1     1     1

K>> asdfasdf(4)=-2.3604 + 0.1536i

asdfasdf =
1.0000 + 0.0000i   1.0000 + 0.0000i   1.0000 + 0.0000i  -2.3604 + 0.1536i
Run Code Online (Sandbox Code Playgroud)

为什么前3个元素突然变得复杂?我怎样才能防止Matlab这样做呢?真实是真的.这不应该只是因为另一个元素是想象的而改变为想象的.

Lui*_*ndo 8

complex属性是数组的属性,而不是每个条目的属性.如果条目需要复杂,那么所有条目都很复杂; 或者更确切地说阵列是.

你说

真实是真的

真实也很复杂.具有零虚部的复数与实数相同(具有相同的值).

数字示例:

>> x = 3; % real number

>> y = complex(3, 0); % force to be complex

>> whos x y % check that x is real and y is complex
  Name      Size            Bytes  Class     Attributes

  x         1x1                 8  double              
  y         1x1                16  double    complex   

>> x==y % are they equal?
ans =
     1
Run Code Online (Sandbox Code Playgroud)

数组示例:

>> x = [2 3 4]; % real values: x is real

>> y = [x, 5+6j]; % include a complex value: y becomes complex

>> x(1:3)==y(1:3) % equal values?
ans =
     1     1     1
Run Code Online (Sandbox Code Playgroud)

  • 只是为了强调`complex`属性是数组的属性,而不是数组中的条目:注意`isreal`返回整个数组的单个输出.`isreal(3)`是真的,`isreal(complex(3,0))`是假的,即使`complex(3,0)`有一个虚部. (3认同)