Upload p2mat.py
Browse files- utils/p2mat.py +20 -0
utils/p2mat.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
import scipy.io
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load the .p file
|
7 |
+
with open('filename.p', 'rb') as file:
|
8 |
+
tensor_data = pickle.load(file) # This should be a PyTorch tensor
|
9 |
+
|
10 |
+
# Ensure it is a PyTorch tensor
|
11 |
+
if isinstance(tensor_data, torch.Tensor):
|
12 |
+
tensor_data = tensor_data.numpy() # Convert to NumPy array
|
13 |
+
|
14 |
+
# Save as a MATLAB file
|
15 |
+
scipy.io.savemat('filename.mat', {'tensor': tensor_data})
|
16 |
+
|
17 |
+
#%% In MATLAB
|
18 |
+
|
19 |
+
# data = load('filename.mat');
|
20 |
+
# tensor = data.tensor; % Access the variable
|