Borrar filtros
Borrar filtros

Find the average between values in a matrix

5 visualizaciones (últimos 30 días)
Alexander
Alexander el 13 de Abr. de 2014
Comentada: Star Strider el 13 de Abr. de 2014
Suppose im given a 1 X n matrix called a and i want to find matrix b which will give the average value of every element of a and its immediate neighbors
for example. a=[0 2 1 1 0].
b=[1 1 1.3 0.667];
Whats a smart way to do this?

Respuesta aceptada

Star Strider
Star Strider el 13 de Abr. de 2014
Editada: Star Strider el 13 de Abr. de 2014
I suggest:
a = [0 2 1 1 0];
for k1 = 1:length(a)-1
ix1 = max(1,(k1-1));
ix2 = min(length(a),k1+1);
m(k1) = mean(a(ix1:ix2));
end
produces:
m =
1.0000 1.0000 1.3333 666.6667
  3 comentarios
Alexander
Alexander el 13 de Abr. de 2014
Also, the majority of your code is correct, however, the k value in the for loop should be
for k1= 1:length(a) other than that it works like a charm.
Star Strider
Star Strider el 13 de Abr. de 2014
My pleasure!
The (k1-1) originated in my initial desire to not get an index-out-of-bounds error. Then I thought of creating the ix1 and ix2 variables and forgot to reset the k1 limit in the for statement. My error.
An -1 index, and an index >length(a) would both throw errors, since neither exists. In order to include [a(1) a(2)] as the first and [a(end-1) a(end)] as the last sub-arrays to average (the others being [a(k1-1) a(k1) a(k1+1)]), the ix1 and ix2 calculations look for the minimum or maximum of the size of the arrays and only use the indices that are within the index range of the arrays. In other words, they prevent the occurrence of indices of (-1) and (end+1). (Since you specified ‘the average value of every element of a and its immediate neighbors’ and illustrated it in your example, that eliminated a(1) alone and a(end) alone being included in the algorithm.)

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 13 de Abr. de 2014
You can try conv():
b = conv(a, [1,1,1]/3, 'full')
b = conv(a, [1,1,1]/3, 'valid')
b = conv(a, [1,1,1]/3, 'same')
Each of the shapes handles the "edge conditions" differently. It depends on what you want to do.
  4 comentarios
Alexander
Alexander el 13 de Abr. de 2014
I was just taking a guess, I was under the impression that conv meant conversion and there 3 values inside the parentheses were points on a plane.
Anyway, this is interesting, I will have to play around with the convolution function. Do you know of any real time application this would be used for? Systems of equations maybe?
Image Analyst
Image Analyst el 13 de Abr. de 2014
Editada: Image Analyst el 13 de Abr. de 2014
Oh my gosh. Convolution is used everywhere! It's the basis of linear systems theory and linear filtering. As just one example, if you have a sharp image and you blur it by the point spread function of the imaging system you will get an image that is the convolution of the image and the point spread function. And the whole basis of the duality of filtering in Fourier (spectral domain) and spatial (or time) domain is built upon convolution. Basically multiplication in the spatial (or time) domain is convolution in the Fourier domain, and multiplication in the spatial (or time) domain is convolution in the Fourier domain. As soon as you have your first course in linear systems, linear filtering, or optics you should get a heavy intensive education in convolution. Here's some background for you:

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by