loop in a 3D matrix

13 views (last 30 days)
Enzo on 19 Jan 2023
Answered: dpb on 19 Jan 2023
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);
enzo on 19 Jan 2023
Moved: dpb on 19 Jan 2023
@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

Sign in to comment.

Accepted Answer

dpb on 19 Jan 2023
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...

More Answers (0)

Community Treasure Hunt

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

Start Hunting!