필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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!

채택된 답변

Akira Agata
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)
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개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by