|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Average contents of a matrixI want to average the contents of a matrix so that each value is the average of all immediately adjacent values and itself: for example if I have a simple 4 by 4 matrix e.g. a[ 1 2 3 4
5 6 7 8 9 10 11 12 13 14 15 16] I would like to create a 4 by 4 matrix av[] so that av[1,1] is the average of a[1,1], a[1,2], a[2,1] and a[2,2]; av[3,2] is the average of a[2,1], a[2,2], a[2,3], a[3,1], a[3,2], a[3,3], a[4,1], a[4,2] and a[4,3] etc... giving a value for av[1,1] of 3.5 and av[3,2] a value of 10 Is there a function that can do this or will I have to loop through doing the calculations or otherwise code it? The purpose of this is to smooth a surface plot of a[] or create a surface plot of the smoothed version of a[], i.e. av[]. |
|
|
Re: Average contents of a matrix> I want to average the contents of a matrix so that each value is the
> average of all immediately adjacent values and itself: for example if I > have a simple 4 by 4 matrix e.g. a[ 1 2 3 4 > 5 6 7 8 > 9 10 11 12 > 13 14 15 16] > > I would like to create a 4 by 4 matrix av[] so that av[1,1] is the average > of a[1,1], a[1,2], a[2,1] and a[2,2]; > av[3,2] is the average of a[2,1], a[2,2], a[2,3], a[3,1], a[3,2], a[3,3], > a[4,1], a[4,2] and a[4,3] etc... > giving a value for av[1,1] of 3.5 and av[3,2] a value of 10 > > Is there a function that can do this or will I have to loop through doing > the calculations or otherwise code it? > > The purpose of this is to smooth a surface plot of a[] or create a surface > plot of the smoothed version of a[], i.e. av[]. If you want to smooth the matrix, may be convolution is what you want. The conv2 function does nearly (but not exactly) what you describe. The difference is the handling at the edges of the matrix. a = some matrix b = [1 1 1;1 1 1; 1 1 1] / 9; a_smoothed = conv2(a, b,'same'); a_smoothed is then the same size as a. b defines the "filter". Or you can look at the filter functions in "Signal processing". - mh _______________________________________________ Help-octave mailing list Help-octave@... https://www-old.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: Average contents of a matrixtir, 03 03 2009 kl. 03:27 +0100, skrev Martin Helm:
> > I want to average the contents of a matrix so that each value is the > > average of all immediately adjacent values and itself: for example if I > > have a simple 4 by 4 matrix e.g. a[ 1 2 3 4 > > 5 6 7 8 > > 9 10 11 12 > > 13 14 15 16] > > > > I would like to create a 4 by 4 matrix av[] so that av[1,1] is the average > > of a[1,1], a[1,2], a[2,1] and a[2,2]; > > av[3,2] is the average of a[2,1], a[2,2], a[2,3], a[3,1], a[3,2], a[3,3], > > a[4,1], a[4,2] and a[4,3] etc... > > giving a value for av[1,1] of 3.5 and av[3,2] a value of 10 > > > > Is there a function that can do this or will I have to loop through doing > > the calculations or otherwise code it? > > > > The purpose of this is to smooth a surface plot of a[] or create a surface > > plot of the smoothed version of a[], i.e. av[]. > > If you want to smooth the matrix, may be convolution is what you want. > The conv2 function does nearly (but not exactly) what you describe. The > difference is the handling at the edges of the matrix. > > a = some matrix > > b = [1 1 1;1 1 1; 1 1 1] / 9; > a_smoothed = conv2(a, b,'same'); > > a_smoothed is then the same size as a. > > b defines the "filter". > > Or you can look at the filter functions in "Signal processing". Or take a look at the 'imsmooth' function in the 'image' package. I provides several different ways of smoothing. Søren _______________________________________________ Help-octave mailing list Help-octave@... https://www-old.cae.wisc.edu/mailman/listinfo/help-octave |
| Free embeddable forum powered by Nabble | Forum Help |