Unable to perform assignment because the left and right sides have a different number of elements.

5 views (last 30 days)
I tried to get max power frequency plot of a data set for rul estimation. I tried the code below many times but i cant fix the error at the below of the code.
Here is the code:
/////////*******/////////
url = 'https://www.mathworks.com/supportfiles/predmaint/condition-monitoring-and-prognostics-using-vibration-signals/pdmBearingConditionMonitoringData.mat';
websave('pdmBearingConditionMonitoringData.mat',url);
load pdmBearingConditionMonitoringData.mat
% Define the number of data points to be processed.
numSamples = length(data);
% It is equal to 600.
% Define sampling frequency.
fs = 20E3;
% Initialize vectors to store the extracted features.
maxPowerFreq = zeros(numSamples,1);
for k = 1:numSamples
% Get most up-to-date data.
curData = data{k};
% Apply median filter.
curDataFilt = medfilt1(curData,3);
% Calculate spectrogram.
[~,fvec,tvec,P_k] = spectrogram(curDataFilt,500,450,512,fs);
% Extract the corresponding frequency value from fvec
[~,I] = max(P_k);
maxPowerFreq(k) = fvec(I);
end
Unable to perform assignment because the left and right sides have a different number of elements.
% Plot the maxPowerFreq
figure;
plot(maxPowerFreq);
title('Max Power Frequency');
------
The Error:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in untitled24 (line 27)
maxPowerFreq(k) = fvec(I);
//////////*********/////////
After i figured out that fvec has 257 elements which can be seen from the below code:
(the data has 600 elements) (numSamples = 600)
///////////*******/////////
url = 'https://www.mathworks.com/supportfiles/predmaint/condition-monitoring-and-prognostics-using-vibration-signals/pdmBearingConditionMonitoringData.mat';
websave('pdmBearingConditionMonitoringData.mat',url);
load pdmBearingConditionMonitoringData.mat
% Define sampling frequency.
fs = 20E3;
% Number of elements to process (assuming at least 3 elements are present).
numElements = min(3, length(data));
% Plot fvec for the first 3 elements in data.
figure;
for k = 1:numElements
% Get current data.
curData = data{k};
% Apply median filter.
curDataFilt = medfilt1(curData, 3);
% Calculate spectrogram.
[~, fvec, ~, ~] = spectrogram(curDataFilt, hann(512), 256, 512, fs);
% Plot fvec for the current element.
subplot(numElements, 1, k);
plot(fvec, 'LineWidth', 2);
title(['Frequency Vector - Element ' num2str(k)]);
xlabel('Index');
ylabel('Frequency (Hz)');
end
/////////////******//////
I dont know what to do. Please help. Thank you.

Answers (1)

Cris LaPierre
Cris LaPierre on 17 Feb 2024
Two ideas come to mind. Both require that the number of points freqturned by freq(I) be the same for every sample.
If you know the number of points you will have, then define maxPowerFreq as zeros(numSamples,numPts). Then, when assigning, specify the row and columns: maxPowerFreq(k,:) = fvec(I);
If you do not know the number of points beforehand, you can use an empty array and concatenation to create maxPowerFreq. I include an example of that approach below.
url = 'https://www.mathworks.com/supportfiles/predmaint/condition-monitoring-and-prognostics-using-vibration-signals/pdmBearingConditionMonitoringData.mat';
websave('pdmBearingConditionMonitoringData.mat',url);
load pdmBearingConditionMonitoringData.mat
% Define the number of data points to be processed.
numSamples = length(data);
% It is equal to 600.
% Define sampling frequency.
fs = 20E3;
% Initialize vectors to store the extracted features.
maxPowerFreq = [];
for k = 1:numSamples
% Get most up-to-date data.
curData = data{k};
% Apply median filter.
curDataFilt = medfilt1(curData,3);
% Calculate spectrogram.
[~,fvec,tvec,P_k] = spectrogram(curDataFilt,500,450,512,fs);
% Extract the corresponding frequency value from fvec
[~,I] = max(P_k);
maxPowerFreq = [maxPowerFreq;fvec(I)];
end
% Plot the maxPowerFreq
figure;
plot(maxPowerFreq);
title('Max Power Frequency');

Community Treasure Hunt

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

Start Hunting!