Alternate to cell array
2 views (last 30 days)
Show older comments
I am working with some codes and My traditional method has been to intialize an array and than put the output of the data into an array, I am doing this for matlabs classification learner and i made the mistake of using an array and hence the output is treated as induvidual features instead of one cell for each feature
clc; clear;
%% Load and truncate the signals
SigA_full = load("F2N_SR_S4_235.mat");
SigB_full = load("F2N_SR_S8_235.mat");
SigA = SigA_full.F2N_SR_S4_235(1:100000, :); % Truncate SigA
SigB = SigB_full.F2N_SR_S8_235(1:100000, :); % Truncate SigB
%% High-pass Filter
fs = 34000; % Hz
filter_freq = 1000; % adjust as per your requirement
filter_freq_norm = filter_freq/(fs/2);
siglen = 50;
p = 1;
for lidx = 1:siglen
%% Load the signal1 and signal2
signal1 = SigA(:, lidx)';
signal2 = SigB(:, lidx)';
%% High-pass Filter
signal1 = highpass(signal1, filter_freq_norm);
signal2 = highpass(signal2, filter_freq_norm);
signal = {signal1, signal2};
signal = signal{1};
%% Calculate features for each signal
% Kurtosis
kurtosisValue = kurtosis(signal);
% Mean
meanValue = mean(signal);
% Standard deviation
sdValue = std(signal);
% Variance
varianceValue = var(signal);
% Skewness
skewnessValue = skewness(signal);
%% Feature array Aggregation
num_bins = 50; % Set bins
bin_averages_signal1 = process_signal(signal1, num_bins);
bin_averages_signal2 = process_signal(signal2, num_bins);
%% Create feature array
features = [kurtosisValue, meanValue, sdValue, varianceValue, skewnessValue, bin_averages_signal1, bin_averages_signal2];
%% Assign the calculated features to the array outside of the loop
f2n3sw2(p,:) = features;
p = p + 1;
end
%%
function bin_averages = process_signal(signal, num_bins)
% Find peaks and their heights
[~, peak_heights] = findpeaks(signal);
% Differences in height between adjacent peaks
peak_height_diffs = diff(peak_heights);
% Calculate the bin size
bin_size = floor(length(peak_height_diffs) / num_bins);
% Array to store the average values of each bin
bin_averages = zeros(1, num_bins);
% Loop through the peak height differences, calculate the average for each bin, and store it in the bin_averages array
for i = 1:num_bins
start_idx = (i - 1) * bin_size + 1;
end_idx = i * bin_size;
bin_data = peak_height_diffs(start_idx:end_idx);
bin_averages(i) = mean(bin_data);
end
end
in this example as you can see me putting the features in a array the feature list is ok until bin_averages_signal1, bin_averages_signal2 where based on the bins it is 50 new cells the issue with this is classfication learner will take it as 50 unique features.
An example for what i want to do is something like this
It would be ok to run the code separately but i would prefer the feature files to be one file instead of having to combine them later as it would be ineffecicent.
2 Comments
Image Analyst
on 24 Jul 2023
I'm having a little bit of trouble following. What variable or columns of variables are your predictors, and what vector is your response (ground truth classification) variable? Please show this screenshot after you have loaded up the variables from your workspace.
Answers (1)
Shivansh
on 13 Sep 2023
Hi Ammar,
I understand that you are working on a classification problem and the output results are not correct because of labelling of predictors in dataset. It seems that you want to combine multiple feature arrays with their corresponding labels into a single training dataset for the classification learner.
You can use the following steps to write a script for creating your dataset:
- Initialize empty arrays for ‘TrainFeatures’ and ‘trainingLabels’ before the loop.
- Inside the loop, concatenate the current feature array with ‘TrainFeatures’ using the vertical concatenation operator ‘vertcat’.
- Concatenate the current labels array with ‘trainingLabels’ using ‘vertcat’ as well.
- After the loop, create a table from ‘TrainFeatures’ using ‘array2table’.
- Concatenate ‘TrainFeaturesTable’ with ‘trainingLabels’ using ‘horzcat’ to create the final training dataset:
The AllFeats contains the combined feature arrays and their corresponding labels in a format suitable for training with the classification learner.
Here's the updated code snippet:
TrainFeatures = [];
trainingLabels = [];
% Loop through each feature array and labels
for i = 1:num_files
currentFeatureArray = % Load the current feature array
currentLabels = % Load the corresponding labels
% Concatenate current feature array with TrainFeatures
TrainFeatures = vertcat(TrainFeatures, currentFeatureArray);
% Concatenate current labels with trainingLabels
trainingLabels = vertcat(trainingLabels, currentLabels);
end
% Convert TrainFeatures to a table
TrainFeaturesTable = array2table(TrainFeatures);
% Concatenate TrainFeaturesTable with trainingLabels
AllFeats = horzcat(TrainFeaturesTable, array2table(trainingLabels));
You can use the ‘AllFeats’ table as your training dataset in the classification learner, where the features and labels are properly combined and organized.
You can refer to following documentations for the functions used in the code:
- vertcat: Concatenate arrays vertically - MATLAB vertcat - MathWorks India
- horzcat: Concatenate arrays horizontally - MATLAB horzcat - MathWorks India
- array2table: Convert homogeneous array to table - MATLAB array2table - MathWorks India
Hope it helps!
0 Comments
See Also
Categories
Find more on Single-Rate Filters 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!