フィルターのクリア

perform an action on part of a column in a matrix

2 ビュー (過去 30 日間)
Tali Labovich
Tali Labovich 2016 年 3 月 30 日
コメント済み: Tali Leibovich 2016 年 3 月 30 日
Hello
I have a matrix of 3 columns:
The first column contains some numbers that I want to average
Second, either 1 or 0.
3rd is empty because I want to insert values to this column
for example:
5 0 0
4 0 0
6 0 0
4 1 0
7 0 0
8 0 0
9 0 0
7 1 0
1 0 0
4 0 0
3 0 0
What I need is to define a range according to the second column (from the first to the last zero)
calculate the average of the numbers in these rows in column 1
copy the average to column 4
For example:
the average of 5,4,6 is 5
and the average of 4, 7, 8, 9 is 7
so it should look like this:
5 0 5
4 0 5
6 0 5
7 1 7
8 0 7
9 0 7
My questions are:
1. How can I define such range (from zero to zero without 1)?
2. How can I insert the calculated value (the average) into every corresponding row?
Thank you so much for your help!
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 3 月 30 日
Can you post the correct expected result?

サインインしてコメントする。

回答 (1 件)

goerk
goerk 2016 年 3 月 30 日
M %your matrix
maskData = cumsum(M(:,2));
for i=0:max(maskData)
mask = maskData==i;
meanValue = mean(M(mask,1));
M(mask,3) = meanValue;
end
  1 件のコメント
Tali Leibovich
Tali Leibovich 2016 年 3 月 30 日
Ingenious! Thank you so much goerk!!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by