Retrieving complex array in Java HashMap is corrupted

3 visualizaciones (últimos 30 días)
Jim Schatzman
Jim Schatzman el 1 de Jun. de 2021
Editada: Sachin Lodhi el 29 de Abr. de 2024
In R2021A, the following code
data = [1+2i,3-4i];
m = java.util.HashMap;
m.put(1,data);
m.get(1)
returns
1
2
In general, it returns the real and imaginary parts of the first half of the data array, as a real array. The second half of the original data array is lost.
Please advise!!

Respuestas (1)

Sachin Lodhi
Sachin Lodhi el 29 de Abr. de 2024
Editada: Sachin Lodhi el 29 de Abr. de 2024
Hello Jim, I understand that while retreiving complex array stored Java Hashmap, you are able to get only first half (specifically only the first element) of the data array.
The behavior you are observing when storing a complex Matlab array in a "java.util.HashMap" and then retrieving it is due to how Matlab and Java interact, especially regarding complex numbers and data type conversion. When you put a Matlab array containing complex numbers into a Java collection, Matlab converts the array into a format that Java understands. Since Java does not have built-in support for complex numbers in the same way Matlab does, the complex array is not preserved as you might expect.
In the case of your code, the result corresponds to the real and imaginary parts of the first complex number in your array. This happens because when the Matlab array is passed to Java, it is converted to a Java array, but the conversion process does not handle complex numbers in a way that retains the full complex data in a single object. Instead, it appears to only process the first complex number and separates its real and imaginary parts, losing the rest of the array.
To work around this issue, you can manually manage the real and imaginary parts of the complex numbers or use a different method to store complex numbers in a Java object. Here are a couple of approaches:
Approach 1: Store Real and Imaginary Parts Separately
You can store the real and imaginary parts as separate entries in the "HashMap" or as separate arrays within a single entry if you want to keep them together.
data = [1+2i, 3-4i]
data =
1.0000 + 2.0000i 3.0000 - 4.0000i
realPart = real(data);
imagPart = imag(data);
m = java.util.HashMap;
m.put(1, realPart);
m.put(2, imagPart);
% Retrieve
realRetrieved = m.get(1)
realRetrieved = 2x1
1 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
imagRetrieved = m.get(2)
imagRetrieved = 2x1
2 -4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
reconstructedData = realRetrieved + 1i*imagRetrieved
reconstructedData =
1.0000 + 2.0000i 3.0000 - 4.0000i
Approach 2: Use a Matlab Container Instead
If retaining the complex data structure within Matlab is more critical and you do not specifically need a Java "HashMap", consider using Matlab's own containers, such as "containers.Map", which can handle complex numbers.
data = [1+2i, 3-4i]
data =
1.0000 + 2.0000i 3.0000 - 4.0000i
m = containers.Map('KeyType', 'double', 'ValueType', 'any');
m(1) = data;
% Retrieve
retrievedData = m(1)
retrievedData =
1.0000 + 2.0000i 3.0000 - 4.0000i
This approach keeps your complex data intact within Matlab's environment, avoiding the data type conversion issues between Matlab and Java. For more information on "containers.Map" please refer to the following documentation :
I hope this helps!

Categorías

Más información sobre Java Package Integration en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by