Help on creating a new column and fill values at specific locations
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Akira Agata
2023-2-14
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!