accumarray function for equal integer values
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Felix Flores James
le 16 Nov 2018
Modifié(e) : Felix Flores James
le 16 Nov 2018
Hey guys,
I have a question about the accumarray function.
I have a table containing columns B1 and B2. B1= [34.1; 34.2; 35.6; 35.7] and B2 shows corresponding intensities and is [600; 800; 200; 100] respectively. My first step is rounding the B1 values to a nominal value, resulting in B1 = [34; 34; 36; 36]. However, in the second step I would like to use the accumarray function for B1 and B2, but this only gives the accumulated B2 column, B2 = [1400; 300]. I would also like to see the B1 column in which duplicate nominal values are now single values. Thus, B1 = [34; 36].
many thanks!
0 commentaires
Réponse acceptée
Bruno Luong
le 16 Nov 2018
B1 = [34.1; 34.2; 35.6; 35.7];
B2 = [600; 800; 200; 100];
Br1 = round(B1);
u = unique(Br1);
[~,J] = ismember(Br1, u);
B2s = accumarray(J(:),B2);
s = [u B2s];
s(J,:)
1 commentaire
Stephen23
le 16 Nov 2018
Felix Flores James's "Answer" moved here and formatted properly:
Bruno,
Thank you for your answer. You're completely right, I should post data/code so that you guys can easily copy and past it in MATLAB. I will do that from now on.
It worked! Thank you!
>> B1 = [34.1; 34.2; 35.6; 35.7];
B2 = [600; 800; 200; 100];
Br1 = round(B1);
u = unique(Br1);
[~,J] = ismember(Br1, u);
B2s = accumarray(J(:),B2);
s = [u B2s];
s(J,:);
>> C = [u B2s]
C =
34 1400
36 300
Plus de réponses (1)
Andrei Bobrov
le 16 Nov 2018
Modifié(e) : Andrei Bobrov
le 16 Nov 2018
B1 = [34.1; 34.2; 35.6; 35.7];
B2 = [600; 800; 200; 100];
B = table(B1,B2);
B.B1 = round(B.B1);
Bnew = varfun(@sum,B,'GroupingVariables','B1');
1 commentaire
Voir également
Catégories
En savoir plus sur Data Type Conversion 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!