How to replace values in a matrix with another set of values

Hi,
I have a 91282 x 1 matrix and I want to replace the specific values 59688:60003 (n=315) with another set of 315 numbers (a 315 x 1 matrix) in those exact places. What is the best way of doing this? Thanks!

2 Comments

Do you mean the values 59688:60003, or are these the indices of the values you want to replace?
Sorry, yeah I meant the indices

Sign in to comment.

 Accepted Answer

Try this
% Stuff sourceVector into indexes 59688:60003 of targetVector
targetVector(59688:60003) = sourceVector;

3 Comments

Yeah that was the first thing I tried but I (expectedly) get "In an assignment A(:) = B, the number of elements in A and B must be the same." because "A" is 91282x1 and B is 315x1. I've been searching tirelessly and can't find a workaround to what should be a simple solution!
@bsriv: Note that 59688:60003 has 316 elements, not 315. So if you want to insert 315 elements starting from element 59688:
len = numel(sourceVector);
targetVector(59688:(59688 + len - 1)) = sourceVector;
% ^^^ essential!

Sign in to comment.

More Answers (1)

Categories

Asked:

on 17 Oct 2018

Commented:

on 18 Oct 2018

Community Treasure Hunt

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

Start Hunting!