Clear Filters
Clear Filters

Arrange the array to give the added output in other array

1 view (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 22 Apr 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

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!