Arrange the array to give the added output in other array

1 vue (au cours des 30 derniers jours)
SUBHO MITRA
SUBHO MITRA le 22 Avr 2022
Commenté : SUBHO MITRA le 22 Avr 2022
In the following array B contains the individual results for the elements in A
Let's say,
A=[2 1 4 2 8 4 1 7 2].';
B=[1 2 3 4 5 6 7 8 9].';
Now, I want A in such a way that It only contains the the unique elements in it, like final A should be:
A=[1 2 4 7 8];
And in final B it contains the total addition of the each individual values of elements in A, so final B will look like:
B=[9 14 9 5 8];
I am looking for a simple code to execute this, it is urgent

Réponse acceptée

Stephen23
Stephen23 le 22 Avr 2022
A = [2;1;4;2;8;4;1;7;2];
B = [1;2;3;4;5;6;7;8;9];
[X,Y,Z] = unique(A);
N = accumarray(Z,B)
N = 5×1
9 14 9 8 5
display(X)
X = 5×1
1 2 4 7 8

Plus de réponses (0)

Catégories

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

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by