Select multiple ranges from a column based on their value and insert the mean of those values into a new matrix

조회 수: 3 (최근 30일)
Hi,
I have a matrix e.g. 20x2 consisting two measurements (M1 and M2). I want to select all M1 as long as M2 is above a value (e.g. from 1-5) and calculate the mean of M2 and insert the resulting mean in a new matrix. Then take the next M2 values (e.g. from 15-20), calculate the mean and add it to the new matrix as long as M1 is above a value. The resulting matrix should include only the mean values based on matrix 1.
How can I do that?

답변 (1개)

KSSV
KSSV 2018년 6월 14일
A = rand(20,2) ; % random data for demo
R = [0. 0.5 ; 0.5 1.] ; % ranges
iwant = zeros(size(R,1),2) ;
M1 = A(:,1) ; M2 = A(:,2) ;
for i = 1:size(R,1)
idx = M2>=R(i,1) & M2<R(i,2) ;
iwant(i,1) = mean(M1(idx)) ;
iwant(i,2) = mean(M2(idx)) ;
end
  댓글 수: 1
toka55
toka55 2018년 6월 14일
Thank you very much. I think my question was unclear. I have a matrix like:
M1 M2
0 3
0.5 4
0.1 5
0 3
0 2
0.3 4
0.2 4
0 2
0.5 2
0.1 5
I want to calculate the mean only if M1 is >0 and insert the means in a new matrix. I would have then:
Mean
4.5
4
3.5
In other words, I want to calculate the mean of all successive values as long as M1 is >0 and insert them in a new matrix.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by