Need help with indexing in a nested for loop

1 次查看(过去 30 天)
Hi everyone. I have to write a code that will apply a narrow band-pass filter around each frequency and stack the ccfs for different test velocities: This is my attempt at writing the code but it gives me the error Assingment has more non-singleton rhs dimensions than non-singleton subscripts. I would appreciate any kind of help. Thank you.
% ccf is a NxM matrix (153x4001), with N the amount of cross-correlation pairs and M the amount of time samples.
%ccf are cross-correlation functions
load ccf
%Define the testing velocity range
testVelocity = 100:1:300;
freqVec = 7:40;
sampling_rate = 400;
% dist is a Nx1 vector with the interstation distance for each of the N pairs
dist = nonzeros(flipud(tril(repmat((85:-5:5)',1,85/5))))';
DispCurve=zeros(length(testVelocity), length(freqVec));
% Loop over frequencies
for fInd = 1:length(freqVec)
freq=freqVec(fInd);
[A,B]=butter(4,[7, 40]./(sampling_rate/2));
ccf_filtered=filtfilt(A,B,ccf); % Narrow band-pass filter applied to each ccf
% Loop over all velocities
for ind = 1:length(testVelocity)
% find the sample in each ccf that corresponds to the test velocity
testVsamplePositiveLag=(M-1)/2 + round(dist./testVelocity(ind));
testVsampleNegativeLag=(M-1)/2 - round(dist./testVelocity(ind));
DispCurve(ind,fInd)=sum(ccf_filtered(:,testVsamplePositiveLag)) + sum(ccf_filtered(:,testVsampleNegativeLag)); % sum positive and negative lag times
end
end

采纳的回答

Star Strider
Star Strider 2019-8-30
The simplest solution is just to add an extra dimension to ‘DispCurve’.
Preallocate as:
DispCurve=zeros(length(testVelocity), length(freqVec), size(ccf,1));
and the assignment in the loop is then:
DispCurve(ind,fInd,:)=sum(ccf_filtered(:,testVsamplePositiveLag)) + sum(ccf_filtered(:,testVsampleNegativeLag)); % sum positive and negative lag times
You can sort out the dimensions later to plot them or whatever you want to do with them.
If you want to keep it as a 2D matrix, adding second sum calls or, if you have a recent MATLAB release, adding 'all' to the sum call will create it as a scalar:
DispCurve(ind,fInd)=sum(ccf_filtered(:,testVsamplePositiveLag),'all') + sum(ccf_filtered(:,testVsampleNegativeLag),'all'); % sum positive and negative lag times
Experiment to get the result you want.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Measurements and Feature Extraction 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by