How to create a .mat file of the following example?
11 views (last 30 days)
Show older comments
I have a data in the following format: (i, j, k, val) where i, j, k are variables and val is the corresponding data at i, j, k values. I want to save the .mat file in the (i, j, k, val) format, so that my .mat files looks like:
(0, 0, 0, 1.5E-02)
(0, 0, 1, 2.5E-05)
...........................
(5, 5, 5, 4.5 E-05)
How can I create this type of .mat file? I would appreciate any help on this.
Actually these data are the octant of a 11 by 11 by 11 matrix (isotropic cubic matrix). I would like to have a full-size matrix using the symmetry property of the octant. I want to save the octant data in a .mat file so that I could iterate over the 8 possibilities to make a full size matrix.
0 Comments
Answers (1)
Deepak
ongeveer 6 uur ago
We can reconstruct a full 11x11x11 matrix from octant data by first saving the octant data in a .mat file, which includes indices and corresponding values. By loading this data, initialize a full-size matrix and iterate over the octant data to fill the matrix using symmetry properties. For each point in the octant, calculate its mirrored positions across the x, y, and z axes, as well as across the xy, xz, and yz planes, and assign the same value to these positions, effectively utilizing symmetry to populate the entire matrix.
Below is the sample MATLAB code to achieve the same:
% Define the octant data
octantData = [
0, 0, 0, 1.5E-02;
0, 0, 1, 2.5E-05;
1, 1, 1, 3.0E-05;
2, 2, 2, 4.0E-05;
5, 5, 5, 4.5E-05;
];
% Save the octant data to a .mat file
save('octant_data.mat', 'octantData');
% Load the data from the .mat file
loadedData = load('octant_data.mat');
octantData = loadedData.octantData;
% Initialize a full-size matrix
fullMatrixSize = 11; % 11x11x11 matrix
fullMatrix = zeros(fullMatrixSize, fullMatrixSize, fullMatrixSize);
% Fill in the octant data and apply symmetry
for idx = 1:size(octantData, 1)
i = octantData(idx, 1) + 1; % MATLAB indices start at 1
j = octantData(idx, 2) + 1;
k = octantData(idx, 3) + 1;
val = octantData(idx, 4);
% Fill the corresponding positions in the full matrix
fullMatrix(i, j, k) = val;
fullMatrix(fullMatrixSize-i+1, j, k) = val; % Symmetry along x
fullMatrix(i, fullMatrixSize-j+1, k) = val; % Symmetry along y
fullMatrix(i, j, fullMatrixSize-k+1) = val; % Symmetry along z
fullMatrix(fullMatrixSize-i+1, fullMatrixSize-j+1, k) = val; % xy-plane symmetry
fullMatrix(fullMatrixSize-i+1, j, fullMatrixSize-k+1) = val; % xz-plane symmetry
fullMatrix(i, fullMatrixSize-j+1, fullMatrixSize-k+1) = val; % yz-plane symmetry
fullMatrix(fullMatrixSize-i+1, fullMatrixSize-j+1, fullMatrixSize-k+1) = val; % xyz symmetry
end
% Display a slice of the matrix to verify
disp('Slice of the reconstructed matrix at z=6:');
disp(fullMatrix(:, :, 6));
Please find attached the documentation of functions used for reference:
I hope this assists in resolving the issue.
0 Comments
See Also
Categories
Find more on Workspace Variables and MAT-Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!