Replace certain elements of vector with the values from another vector (average the values belonging the same position)

조회 수: 1 (최근 30일)
If A = [0 0 0 0 0] and B = [5 6 4] and C = [2 5 2] then
how can I get average of two values ​​belong to position 2nd in final answer that menas final answer should like A = [0 4.5 0 0 6]?

채택된 답변

KSSV
KSSV 2020년 6월 8일
편집: KSSV 2020년 6월 8일
A = [0 0 0 0 0] ;
B = [5 6 4] ;
C = [2 5 2] ;
[c,ia,ib] = unique(C) ;
for i = 1:length(c)
A(C(ib==i)) = mean(B(ib==i)) ;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by