-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertDatasetToMatlabSubspaceFormat.m
More file actions
29 lines (26 loc) · 1.26 KB
/
convertDatasetToMatlabSubspaceFormat.m
File metadata and controls
29 lines (26 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function convertedData = convertDatasetToMatlabSubspaceFormat(data, labels)
% ensure that the labels are categorical
labels = categorical(labels);
% find the uniqu labels in the training data
uniqueLabels = unique(labels);
numClasses = numel(uniqueLabels);
[numFeatures, ~] = size(data);
% Convert the integers 0 to 9 to categorical for comparison
categories = arrayfun(@(x) categorical(x), 0:(numClasses-1), 'UniformOutput', false);
% Use cellfun instead of arrayfun because we're now dealing with cell arrays
samplesPerClass = cellfun(@(x) sum(labels == x), categories);
% Find the max number of samples in any class to determine the size of the 3D array
maxSamples = max(samplesPerClass);
% Initialize the 3D matrix with zeros
newdata = zeros(numFeatures, maxSamples, numClasses);
% Fill the 3D matrix
for class = 0:(numClasses-1)
classSamples = data(:, labels == categorical(class));
% You can choose to replicate the data if there are fewer samples than maxSamples
% or just fill the matrix with the available samples leaving zeros otherwise
numClassSamples = size(classSamples, 2);
newdata(:, 1:numClassSamples, class + 1) = classSamples;
% If you want to replicate samples for classes with fewer samples, add that logic here
end
convertedData = newdata;
end