Overwriting a part of an array

I have arrays of fixed size, and starting at a fixed location, I want to overwrite the values in the array with values of another array of variable length.
For example:
A = [1 3 4 2 6 7 4 6 7]
B1 = [9 8 5]
B2 = [5 5 4 3]
I want to overwrite the values in A, starting at position 2, with the values of B. (B is always shorter than A) This would yield:
C1 = [1 9 8 5 6 7 4 6 7]
C2 = [1 5 5 4 3 7 4 6 7]
One way to achieve this is:
C1 = A;
C1(2:1+size(B1,2)) = B1;
I was wondering if there is a more direct/elegant way, that doesn't require the size(B1,2) parameter?

 Accepted Answer

C1 = A;
C1(2:1+length(B1)) = B1;
You have to mention the indices where you are replacing, so you are bound to use size/ length.

1 Comment

hello, i have question, wht is i what to replace just a value and my matrix is zeros's matrix... for example
med=5
X=3
A=zeros(1,med)
and i want to change only X in the position (1,3) and save that value

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!