Arrange the array to give the added output in other array

1 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2022-4-22
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by