Why am I getting two different data arrays with the same set of data?

3 visualizaciones (últimos 30 días)
The title is a bit ambiguous, but I will try my best to explain.
I have a 4-D complex data set (see attached).
First, I'm writing this to a text file.
clear; clc
load('modes_f_0.mat');
nb_modes_om_e = length(modes_om_e);
nb_modes_om_o = length(modes_om_o);
nb_heading_genmds = length(heading_genmds);
% check if modes_om_e(1) <=0
if (modes_om_e(1) < -1.D-06)
flag = 1;
nb_modes_om_e = nb_modes_om_e-1;
else
flag = 0;
end
% Open the file for writing
fileID = fopen('modes_f_0.txt', 'w');
% Check if the file was opened successfully
if fileID == -1
error('Unable to open the file for writing.');
end
% Write the data to the file
for i=1:nb_modes_om_e
for j=1:nb_modes_om_o
for k=1:nb_heading_genmds
for m=1:6
fprintf(fileID, '% .13e % .13e\n', real(modes_f_0(i,j,k,m)), imag(modes_f_0(i,j,k,m)));
end
end
end
end
% Close the file
fclose(fileID);
Next, I wanted to recheck the data, so I read the text file, and recreate the 4D matrix.
clear;
load('modes_f_0.mat');
nb_modes_om_e = length(modes_om_e);
nb_modes_om_o = length(modes_om_o);
nb_heading_genmds = length(heading_genmds);
nb_modes_om_e = nb_modes_om_e-1; % Similar to the step earlier (% check if modes_om_e(1) <=0)
% Open the text file for reading
fileID = fopen('modes_f_0.txt', 'r');
% Check if the file was opened successfully
if fileID == -1
error('Unable to open the file for reading.');
end
modes_f_0_2 = zeros(nb_modes_om_e, nb_modes_om_o, nb_heading_genmds, 6);
for i = 1:nb_modes_om_e
for j = 1:nb_modes_om_o
for k = 1:nb_heading_genmds
for m = 1:6
% Read real and imaginary parts
real_part = fscanf(fileID, '%f', 1);
imag_part = fscanf(fileID, '%f', 1);
% Store the values in the array
modes_f_0_2(i, j, k, m) = complex(real_part, imag_part);
end
end
end
end
% Close the file
fclose(fileID);
I was under the impression that I can recreate the same data set where I began (or the one I used to write the text file with). But the two data sets, modes_f_0 and modes_f_0_2 are different. What am I doing wrong?
PS: I have a sample script to check this, but I don't think that's helpful. I'll include it here regardless.
data1 = modes_f_0_2; % First dataset
data2 = modes_f_0; % Second dataset
% Check if the datasets are equal
if isequal(size(data1), size(data2))
% Perform element-wise inequality comparison
unequalElements = (data1 ~= data2);
% Find the indices where the differences occur
diffIndices = find(unequalElements);
if isempty(diffIndices)
disp('The datasets are equal.');
else
disp('The datasets are not equal.');
% Display the indices
disp('Indices where differences occur:');
disp(diffIndices);
end
else
disp('The datasets have incompatible sizes.');
end

Respuesta aceptada

Mrinal Anand
Mrinal Anand el 16 de Jun. de 2023
I tried running your sample testing script and it says the datasets have incompatible sizes. This could be because you are running a check in the first code which reduces one of the dimensions of the 4D matrix:
if (modes_om_e(1) < -1.D-06)
flag = 1;
nb_modes_om_e = nb_modes_om_e-1;
But there is no similar check in the recreation code. This could result in different dimensions for the reconstructed matrix and hence resulting in datasets being different.
  1 comentario
Jake
Jake el 16 de Jun. de 2023
Yup. I was too silly to not see this. I should've spent a bit more time than posting here. Apologies!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by