How to create code that runs on all columns?
5 views (last 30 days)
Show older comments
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)
Accepted Answer
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
dpb
on 31 Dec 2020
BTW, if you have Image Processing TB (I don't), there's blockproc you may find advantageous.
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!