Help on creating a new column and fill values at specific locations
3 views (last 30 days)
Show older comments
Anastasiia Khibovska
on 14 Feb 2023
Answered: Akira Agata
on 14 Feb 2023
Hello!
I have three column vectors:
- one is the message type (1-15)
- another is the start time when the message is desplayed (0 until end of trials)
- 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 Comments
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)
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 Comments
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!