Incompatible Indices when updating matrix in the loop

3 views (last 30 days)
Hey everyone!
I am doing some ECG analysis. I am trying to create a 2D-array that is updated for every loop iteration, but I get the following error when I run the code "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side." Any idea on how to fix it?
Note: I know it is bad practice to not preallocate memory for matrices, but I am dealing with very large amounts of data and I cannot find its size.
%% Frequency Domain Analysis
fs3 = 4; % 4 hertz sampling frequency
xdft = [];
PowerSpectralDensity = [];
for i = 0: 60: (60-1)*240
starttime2 = i;
finishtime2 = starttime2 + 60;
StartElement2 = starttime2 *fs+1;
FinishElement2 = finishtime2 * fs;
minuteSubset2 = ECG (StartElement2 : FinishElement2);
t3 = starttime2: 1/fs3: finishtime2 - 1/fs3;
%resampling the data to frequency = 4
minuteSubset2_rs = resample(minuteSubset2, fs3, fs);
N = length(minuteSubset2_rs);
xdft = fft(minuteSubset2_rs');
PowerSpectralDensity = (1/(fs3*N))* abs(xdft).^2;
PowerSpectralDensity = PowerSpectralDensity(1:N/2);
for j = 1: 240
PDarray(i/60 +1, j) = xdft; %error in this line
end
end

Accepted Answer

William Rose
William Rose on 15 Dec 2021
On the line in question, you are assigning vector xdft (right hand side) to one element of array PDarray(). That won't work. You must assign a vector to a row or column of an array, and the sizes must match.
Please provide values for fs and array ECG(), if you want others to be able to run this code.
  4 Comments
Mouath Abu-Daoud
Mouath Abu-Daoud on 16 Dec 2021
Thank you so much, this was really helpful!
Yeah I am resampling at 4 Hz as it would be useful later on for detecting apnea. If you want I will update you with the results.
William Rose
William Rose on 16 Dec 2021
@Mouath Abu-Daoud, I'm glad that my answer was helpful. Good luck with your work!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!