Need to create a code for the following statement:
let a=[1 3 5 6 6 7 7 7] , b=[81 122 245 329 656 790 850 1023]
c=[1 1 1 2 3] where c is showing how many times a unique value of a has been repeated e.g. '1' is repeated '1' time, '6' is repeated '2' times and so on.
Now what I need to do is to create d such that if 1st index of c is 1 then assign 1st index value of b to d and if 1st index of c is 2 then assign 1st and 2nd index value of b to d.
In short what I need is d which will have value d=[81 122 245 329+656 790+850+1023].
Will appreciate your help:D

 채택된 답변

Stephen23
Stephen23 2020년 3월 25일

0 개 추천

>> [~,~,X] = unique(a);
>> d = accumarray(X(:),b(:))
d =
81
122
245
985
2663

댓글 수: 3

Raza Hassan
Raza Hassan 2020년 3월 26일
Thanks alot. worked just fine, only thing is that I was hoping for a code rather than a built-in command to get the required result. Hope you can help me in this regard.
>> [~,~,x] = unique(a);
>> d = zeros(1,max(x));
>> for k = 1:numel(b), d(x(k)) = d(x(k)) + b(k); end
>> d
d =
81 122 245 985 2663
Raza Hassan
Raza Hassan 2020년 3월 26일
Thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2020년 3월 25일

댓글:

2020년 3월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by