Incompatible Indices when updating matrix in the loop
4 views (last 30 days)
Show older comments
Mouath Abu-Daoud
on 15 Dec 2021
Commented: William Rose
on 16 Dec 2021
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
0 Comments
Accepted Answer
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
William Rose
on 16 Dec 2021
@Mouath Abu-Daoud, I'm glad that my answer was helpful. Good luck with your work!
More Answers (0)
See Also
Categories
Find more on Spectral Estimation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!