loop in a 3D matrix
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello everyone,
I have a 3D matrix, where the first dimension represent the frequencies, ranging from 0.5 to 90.5 Hz for a total of 60 frequencies, the second dimension which represents teh actual values, and the third dimension (thousands, up to 5000), which represents the trials (120).
the final matrix should look something like that: 60x5300x120
let's assume cwt_matrix (in line 1) has the aformentioned dimensions and values 60x5300x120
i would like to do a for loop which includes, for every given frequency, the mean and the total power (avpow_sing and avpow_av_sing.) and then use these values in order to plot these using imagesc or surface. The final goal is to display a time-frequency plot.
These operations are summarized in the following lines of code (at the bottom, you will find my attempt to perform this loop, which is a failure):
%these are the values and the computation required
cwt_matrix_2d_sing = cwt_matrix(1, :, :);
cwt_matrix_2d_sing = squeeze(cwt_matrix_2d_sing(1,:,:));
avpow_sing = abs(cwt_matrix_2d_sing).^2;
avpow_av_sing = mean(avpow_sing,2);
%this is my try (failed)
num_freq = 60;
for ij = 1:num_freq
cwt_matrix_1(ij) = cwt_matrix(ij, :, :);
cwt_matrix_1(ij) = squeeze(cwt_matrix_1(1,:,:));
avpow(ij) = abs(cwt_matrix_1(ij)).^2;
avpow_av(ij) = mean(avpow(ij),2);
end
댓글 수: 2
dpb
2023년 1월 19일
What do you think your resultant array size should be -- what do you want to average over -- the 3rd dimension for each frequency or the second? It's not clear precisely what the independent variable along the second dimension is from the description; if the first is frequency, then the spectrum would be along the first dimension for a given observation, it would seem.
enzo
2023년 1월 19일
이동: dpb
2023년 1월 19일
@dpb i would like to have, for every of the 60 frequency, a 5000x1 matrix (or 1×5000, i need the values to be disposed along the rows). The 5000 is the results obtained by averaging the 120 trials. The frequencies, which spam from 0.5 to 90.5 Hz,and subdived into 60 frequency bands (each frequency bads conprises 1.5 Hz band). Therefore, the for loop should computer the mean as mentioned above for everyone of the 60 frequencies. But first, you have to raise to the square power and abs the values (as showed in my first lines of code). Thanks for your help
채택된 답변
dpb
2023년 1월 19일
OK, that's kinda' what I thought but wasn't sure...to average each frequency over the 120 planes, you don't need any loops at all, just operate over the 3rd dimension of the 3D array as is...
meanResp=mean(cwt_matrix,3); % average over trials (planes)
meanPow=mean(abs(cwt_matrix),3); % mean abs spectrum (presuming is complex result from fft)
Depending on how you obtained cwt_matrix, it may need to be divided by length of signal to normalize the FFT...
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!