# Help on creating a new column and fill values at specific locations

1 view (last 30 days)
Anastasiia Khibovska on 14 Feb 2023
Answered: Akira Agata on 14 Feb 2023
Hello!
I have three column vectors:
1. one is the message type (1-15)
2. another is the start time when the message is desplayed (0 until end of trials)
3. another is vector with each row being each second of the trial (total 2000 seconds/rows)
However, what I want to do is make a new column vector that is the same size of the vector 3 (all time points) with values from vector 1 inserted in positions that corresponds to each timing of the message.
For example:
1 = [1,2,3,4]
2 = [3,6,8,10]
3 = [0,1,2,3,4,5,6,7,8,9,10]
new vector = [0,0,1,1,1,2,2,3,3,4]
Does this make sense?
Thank you so much for any answer!
##### 0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

### Accepted Answer

Akira Agata on 14 Feb 2023
How about the following?
% Example
v1 = 1:4;
v2 = [3, 6, 8, 10];
v3 = 1:10;
% Create the new vector
newVec = zeros(size(v3));
newVec(v2) = 1;
newVec = cumsum(newVec);
% Show the result
disp(newVec)
0 0 1 1 1 2 2 3 3 4
If the 1st vector is NOT the 1:N, some additional process is needed, like:
% Example
v1 = [1, 4, 2, 3]; % <- not the 1:N
v2 = [3, 6, 8, 10];
v3 = 1:10;
% Create the new vector
newVec = zeros(size(v3));
newVec(v2) = 1;
newVec = cumsum(newVec);
idx = newVec == 0;
newVec(idx) = 1;
newVec = v1(newVec);
newVec(idx) = 0;
% Show the result
disp(newVec)
0 0 1 1 1 4 4 2 2 3
##### 0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by