Is there a function that performs similar to 'accumarry' but multiplies instead of accumulates?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mnr
am 23 Feb. 2016
Beantwortet: Star Strider
am 23 Feb. 2016
Hello all, Is there a function that performs similar to 'accumarry' but multiplies instead of accumulates? Thank you!
0 Kommentare
Akzeptierte Antwort
Star Strider
am 23 Feb. 2016
You can multiply with accumarray if you specify the prod function as the function you want it to use. This is an adaptation of a recent Answer that wanted to take the mean of values in the second column of the matrix that shared the same value of the first column, so I originally used the mean function. Here, I’m multiplying them instead, using the prod function:
ExCellArray = {[1 3.1; 2 4.5; 3 5.7; 4 8.2; 4 5.2]};
ExArray = cell2mat(ExCellArray);
[UExA, ia, ic] = unique(ExArray(:,1));
Out = [ExArray(ia,1) accumarray(ic, ExArray(:,2), [], @prod)]
Out =
1 3.1
2 4.5
3 5.7
4 42.64
The accumarray function can do a lot of neat stuff if you just ask it nicely!
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Types finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!