# column-wise operation based on indices, accumarray?

1 visualizzazione (ultimi 30 giorni)
hamed amini il 3 Feb 2021
Risposto: Walter Roberson il 3 Feb 2021
if s and v are matrices of the same sizes.
s = [1 1
1 2
2 2
1 2
2 2
3 1
3 1];
v = [10 10
20 7
5 13
60 2
9 6
8 3
3 5];
For each column, find the mean of values of elements in matrix v with the same index in matrix s and replace the values with the mean.
m = [30 6
30 7
7 7
30 7
7 7
5.5 6
5.5 6]:
I know how to use unique and for loop to get the answer. I was wondering if there is a neater way.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Walter Roberson il 3 Feb 2021
accumarray(s(:, 1),v(:,1),[],@mean)
Now do that for each column.
Where I wrote [] use [max(s(:)), 1] . Note this is not max of the column, it is overall maximum.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by