How to create code that runs on all columns?

I am trying to run this code on all columns in my dataset.
n = 167;
b = arrayfun(@(i) mean(A.Echo1_500kHz.volumeBackscatter_dB(i:i+n-1)),1:n:length(A.Echo1_500kHz.volumeBackscatter_dB)-n+1)';
I need the code to also be able to be used on data sets of varying column lengths.
I have tried and failed at trying to create a loop. Im sure it is simple however I am really strugling!
The below code is what I am currently running for my pcolor plot. I would like to average 167 0.006m depth bins into 1m bins. The aim is to show the differnece in resolution between two oceanographic instruments. One that samples at 0.006m and does no processing and one that also samples at the same rate but averages into 1m bins instead of recording the raw data.
load('2CawsandDevil0.006-11.23.28-12.20.29.mat')
d=datestr(A.Echo1_500kHz.timestamp)
t=datetime(d)
t=t.'
dd1=downsample(A.Echo1_500kHz.volumeBackscatter_dB,167)
de1=downsample(A.Echo1_500kHz.binDepth_m,167)
subplot (4,1,1)
A.Echo1_500kHz.timestamp
f1=pcolor(t,A.Echo1_500kHz.binDepth_m,A.Echo1_500kHz.volumeBackscatter_dB)
%shading interp
set(f1, 'EdgeColor', 'none');
colormap jet
caxis([100,140])
colorbar
set(gca,'ydir','reverse')
ylim([0,35])
ssection=datetime(2020,11,10,12,12,00)
esection=datetime(2020,11,10,12,20,00)
xlim([ssection,esection])
grid on
ylabel ('Depth (m)','fontsize',14)
xlabel('','fontsize',14)
title('2CawsandDevil data 0.006m')
set (gca,'fontsize',14)
x=datetime(2020,11,10,11,36,08)
x1=datetime(2020,11,10,12,17,08)

4 Comments

Tell us what are you trying to compute and what are the inputs instead of trying to decipher nonworking code...
You probably don't need a loop at all.
Thanks for your response.
This code works perfectly but will only run on the first column. I would like it to repeat itself on every column.
I have a data set of 10583x3352 single. Where the the y axis is depths in 0.006m intervals and the x is the dB of return signal from an Echo Sounder. I use this with a timestamp variable to create a pcolor plot.
I need to averge every 167 rows into 1 row for every column.
The end result should be a comparable pcolor plot with depth intervals of 1m (comprised of the averge dB values of 167 rows) rather than 0.006m.
This will result in a plot that is has greatly reduced vertical resolution.
I hope this helps.
Unfortunately as when compressed all of the data needed to run my code is 200mb so I cannot upload the data. I have however just uploaded my pcolor plot code to hopefully allow you to see what I have done and what I would like to do with my other plot.

Sign in to comment.

 Accepted Answer

dpb
dpb on 31 Dec 2020
Edited: dpb on 31 Dec 2020
"I have a data set of 10583x3352 single ... I need to averge every 167 rows into 1 row for every column."
N=167; % the averaging accumulator value
Zavg=splitapply(@mean,Z,ceil([1:size(Z,1].'/N))); % average in groups of N
NB: Can't use the trick of reshape() to average of array of N rows here because your array size of 10583/N = 63.37... The above leaves a group of 10583 - 63*167 = 10583 - 10521 = 62 at the end. You can choose to deal with this as wish; above is most general approach keeping all the data.
NB2: You never need to upload a humongous data file to illustrate with...algorithms are as good for small arrays as are for large; the actual number is immaterial. Example of the above used to get syntax right here --
>> Z=rand(23,2)
Z =
0.37 0.08
0.58 0.84
0.25 0.43
0.53 0.85
0.72 0.96
0.47 0.06
0.98 0.27
0.82 0.36
0.38 0.54
0.95 0.62
0.88 0.58
0.96 0.51
0.03 0.79
0.77 0.47
0.34 0.13
0.53 0.94
0.35 0.26
0.69 0.23
0.28 0.97
0.77 0.99
0.69 0.05
0.75 0.09
0.55 0.51
>> N=5;
>> splitapply(@mean,Z,ceil([1:size(Z,1)].'/N))
ans =
0.49 0.63
0.72 0.37
0.59 0.50
0.52 0.68
0.66 0.22
K>>

3 Comments

Thomas Blacklock
Thomas Blacklock on 31 Dec 2020
Edited: dpb on 31 Dec 2020
Thanks for your answer. I had no idea you could use a single line to apply to a whole data set. This helps so much!
dpb
dpb on 31 Dec 2020
Edited: dpb on 31 Dec 2020
That's what MATrix LABoratory is all about...matrix/array operations.
The "trick" here is to build the grouping variable over which to operate.
BTW, if you have Image Processing TB (I don't), there's blockproc you may find advantageous.

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Asked:

on 31 Dec 2020

Commented:

dpb
on 31 Dec 2020

Community Treasure Hunt

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

Start Hunting!