Mean of certain elements within a matrix

28 visualizaciones (últimos 30 días)
Malik Khan el 11 de Dic. de 2019
Comentada: RAMPUNIT KUMAR el 1 de Sept. de 2021
I have a matrix which is a 372x6. Rather than find a mean for a specific row or a specific column, I want to find the mean of elements that lay in different rows as
Although the elements are from different rows, I have a pattern for the order in which they are to be taken. The last 2 elements (column 5 and 6) is to be averaged with the second third and fourth of the following row. The mean then needs to be found for each line. To make this easier to visualise I have written out a matrix for which we need to find 2 means. We need one mean to be of (e f h i j) and the other (k l m n o). For my case however I need to do this 371 times as we have 372 lines (obviously we can’t find mean for the first row).
(a b c d e f)
(g h i j k l)
(l m n o p q)
1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
RAMPUNIT KUMAR el 1 de Sept. de 2021
I too have a doubt, like for matrix
(a b c d e f g h I j k l m n o p q r)
I need to find the mean of a,b then c,d then e,f then g,h and so on upto last element. How could we do that if size is big enough.

Iniciar sesión para comentar.

Stijn Haenen el 11 de Dic. de 2019
I think this will work:
for i=1:371
Mean_i(i)=mean([matrix(i,5:6),matrix(i+1,2:4)]);
end
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Más respuestas (2)

Bandar el 11 de Dic. de 2019
Editada: Bandar el 11 de Dic. de 2019
A=[1 2 3 4 5 6; 7 8 9 10 11 12;13 14 15 16 16 18;19 20 21 22 23 24]
[row,~]=size(A);
for i=1:row-1
c = A(i,[5 6]);
r = A(i+1,[2 3 4]);
B = [c r]
mean(B)
end
1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
RAMPUNIT KUMAR el 1 de Sept. de 2021
RAMPUNIT KUMAR less than a minute ago ⋮ I too have a doubt, like for matrix (a b c d e f g h I j k l m n o p q r) I need to find the mean of a,b then c,d then e,f then g,h and so on upto last element. How could we do that if size is big enough.

Iniciar sesión para comentar.

Andrei Bobrov el 11 de Dic. de 2019
Editada: Andrei Bobrov el 11 de Dic. de 2019
Here A - your array (m x 6)
[m,n] = size(A);
i = repmat((0:m-1)',1,n-1);
i(:,end-1:end) = circshift(i(:,end-1:end),-1);
i = [zeros(m,1),i];
lo = i ~= 0;
out = accumarray(i(lo),A(lo),[],@mean);
or
[m,n] = size(A);
i = [zeros(m,1),reshape(circshift(repelem(0:m-1,n-1),-2),n-1,[])'];
out = accumarray(i(i>0),A(i>0),[],@mean);
1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
RAMPUNIT KUMAR el 1 de Sept. de 2021
RAMPUNIT KUMAR less than a minute ago ⋮ I too have a doubt, like for matrix (a b c d e f g h I j k l m n o p q r) I need to find the mean of a,b then c,d then e,f then g,h and so on upto last element. How could we do that if size is big enough.

Iniciar sesión para comentar.

Categorías

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

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by