MATLAB Answers

average vector over selected areas

1 view (last 30 days)
Lev Mihailov
Lev Mihailov on 3 Feb 2020
Edited: Image Analyst on 4 Feb 2020
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?

Answers (1)

KSSV
KSSV on 3 Feb 2020
Edited: KSSV on 4 Feb 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 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by