Effacer les filtres
Effacer les filtres

perform an action on part of a column in a matrix

2 vues (au cours des 30 derniers jours)
Tali Labovich
Tali Labovich le 30 Mar 2016
Commenté : Tali Leibovich le 30 Mar 2016
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 commentaire
Azzi Abdelmalek
Azzi Abdelmalek le 30 Mar 2016
Can you post the correct expected result?

Connectez-vous pour commenter.

Réponses (1)

goerk
goerk le 30 Mar 2016
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 commentaire
Tali Leibovich
Tali Leibovich le 30 Mar 2016
Ingenious! Thank you so much goerk!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by