average vector over selected areas

I have two vectors, one working, the second sections that I need to average in vector 1
A = randi([5 10],1,548);
B= [102 105 204 99 ]
% what i need to do x=mean(A(1:B(1)) ; x=mean(A(B(1):B(2))
how do i do this?

Respuestas (1)

KSSV
KSSV el 3 de Feb. de 2020
Editada: KSSV el 4 de Feb. de 2020
A = randi([5 10],1,548);
B= [1 102 105 204]
% what i need to do x=mean(A(1:B(1)) ; x=mean(A(B(1):B(2))
iwant = zeros(length(B)-1,1) ;
for i = 1:length(B)-1
if B(i) < B(i+1) ;
iwant(i) = mean(A(B(i):B(i+1))) ;
else
iwant(i) = mean(A(B(i):-1:B(i+1))) ;
end
end

3 comentarios

Lev Mihailov
Lev Mihailov el 3 de Feb. de 2020
Editada: Image Analyst el 4 de Feb. de 2020
mean(A(B(i):B(i+1)))
% B(1):B(1+1) = B(1):B(102)
% % and if I have a value in the vector will be [5 200 5 104 6]
Image Analyst
Image Analyst el 3 de Feb. de 2020
Editada: Image Analyst el 4 de Feb. de 2020
Lev, what indexes in A do you want to average if B(i+1) is LESS than B(i)? Like 200 to 5, or 104 to 6? Do you want to take the mean of from 5 to 200 (the opposite order), inclusive? In other words, sort the numbers in ascending order first?
KSSV
KSSV el 4 de Feb. de 2020
Edied the answer.

La pregunta está cerrada.

Productos

Preguntada:

el 3 de Feb. de 2020

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by