Simple Math With Complex Arrays
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Matthew Reed
el 23 de Oct. de 2018
Editada: James Tursa
el 23 de Oct. de 2018
I seem to be unable to do some basic arithmetic with complex numbers in matlab.
When I define the arrays,
B=complex(cos(A),sin(A));
C=complex(cos(A),sin(A));
I get complex arrays. A is real. When I then define a pair of vectors,
i=2:M-1;
j=2:N-1;
To make the following calculation,
C(i,j) = (B(i-1,j)-B(i+1,j))/(2*dx)
or this,
C(i,j)= complex(real((B(i-1,j)-B(i+1,j))/(2*dx)),imag((B(i-1,j)-B(i+1,j))/(2*dx)));
C(i,j) is real. This is just subtraction. Does there exist some way to do basic arithmetic with complex arrays? Or do I need to break these arrays up and then define my own algebra?
3 comentarios
Respuesta aceptada
Matthew Reed
el 23 de Oct. de 2018
Editada: Matthew Reed
el 23 de Oct. de 2018
1 comentario
James Tursa
el 23 de Oct. de 2018
Editada: James Tursa
el 23 de Oct. de 2018
So, you have discovered the value of creating a small working subset of code that reproduces the problem. Had you posted that example at the outset, we could have seen right away the 0+0i issue and advised you what the issue was. MATLAB routinely converts the results of operations and function calls to strict real if the imaginary part is all 0's.
Más respuestas (1)
James Tursa
el 23 de Oct. de 2018
Editada: James Tursa
el 23 de Oct. de 2018
So, MATLAB has functions i and j defined as the imaginary sqrt(-1) values. I notice that you are using i and j as indexing. This will shadow these MATLAB i and j functions. If you have any code that uses i and j assuming they are the sqrt(-1) value but instead get your indexing vectors, this will of course will not work. I would recommend changing your indexing variable names to something else, like x and y or maybe I and J so that there isn't a potential conflict. And rewrite any code you wrote that uses i and j for sqrt(-1) to instead use 1i and 1j.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!