problem in calculating mean from the required element of matrix??

1 visualización (últimos 30 días)
NAVNEET NAYAN
NAVNEET NAYAN el 5 de Mayo de 2017
Editada: KL el 5 de Mayo de 2017
I have a matrix mat6 of dimension 1-by-210. j =210. I am applying a threshold condition on mat6 and then i need to calculate the mean of next twenty elements . I want that the mean should be calculated from that first element of mat6 at which threshold condition was met at first time. But it is calculating mean from that last element of mat6 at which the threshold condition was met.
for k = 1:j-20
if mat6(k)>0.5
w=k+20;
mea = sum(mat6(1,k:w))/20;
end
end
can anyone please sort out this problem ??
Thanks
Navneet Nayan

Respuesta aceptada

KL
KL el 5 de Mayo de 2017
Editada: KL el 5 de Mayo de 2017
k = 1;
while k <j-20
if mat6(k)>0.5
mea = mean(mat6(1,k:k+19))
k=k+20;
else
k = k+1;
end
end

Más respuestas (1)

Jan
Jan el 5 de Mayo de 2017
Editada: Jan el 5 de Mayo de 2017
index = find(mat6 > 0.5, 1); % Find 1 index only
mea = mean(mat6(index:index+19));
Consider that index+19 might exceed the data size. Handle this situation e.g. by an error, e.g.:
index = find(mat6 > 0.5, 1);
if index + 19 > size(mat6, 2)
error('Index out of range in mat6');
end
  6 comentarios
NAVNEET NAYAN
NAVNEET NAYAN el 5 de Mayo de 2017
tell me one thing Jan, if i want to use the subsequent indices for eg. i want to use the second or 3rd index, how can I proceed??
KL
KL el 5 de Mayo de 2017
Editada: KL el 5 de Mayo de 2017
index = find(mat6 > 0.5);
%this finds all the indices and then you could follow Andrei's command. (with the error check by Jan)
mea = mean( mat6( bsxfun(@plus,index(:)',(0:19)')) );

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by