Replace certain elements of vector with the values from another vector

조회 수: 34 (최근 30일)
Katja
Katja 2013년 11월 21일
답변: Olawale Oyewole 2023년 6월 16일
Hi.
I could use some help here.
There is a zeros array, where I would like to replace some elements that have certain positions with the values from another vector. So, if I have:
A=[0 0 0 0 0] %Zeros vector
B=[5 6] %values
C=[2 5] %positions of elements in A that should receive a new value from B
How should I get a vector D=[0 5 0 0 6] ?
I appreciate your helping!

채택된 답변

Iain
Iain 2013년 11월 21일
  댓글 수: 2
Katja
Katja 2013년 11월 22일
I'm amazed! It's a very elegant solution! In my real case things a bit more complicated, but I think it will work. Thanks a lot, lain!
TAPAN PATEL
TAPAN PATEL 2020년 6월 8일
ok, Now I have one question:
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]?

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

추가 답변 (1개)

Olawale Oyewole
Olawale Oyewole 2023년 6월 16일
for i=1:3
x=[1 2 5];
y=zeros(1,3);
y(1:1:3)=1+log(x(i));
end
Please help replace each entry of y by 1+log(x(i)). That is I want y=[0 0 0] changed to y=[1+log(x(1)) 1+log(x(2)) 1+log(x(3))]

카테고리

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