eding numbers in the matrix
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I have matrices with logfile and data. The first 5 columns is the log fille that logs stimuli representation in time (an example, attached below ). The rows are time and the columns are events. The first column logs stimuli id in time. (No stimulus =0, stimulus : 1 2 3 4 5 or 6) The second column logs the trial stages(1=pre stimulus, 2=stimulus, 3=poststimulus interval); However stimulus durations are not exactly the same for all stimuli. Some have 40 rows some 41. I need make all stimuli the same length by adding missing stimuli ids at the end of the stimulus. For example, if some stimuli are 40 rows and some are 41 or 42rows. make all stimuli to 42 rows by edding stimulus ids at the end of the stimulus in colomn 1 and column 2 Could anyone help with this?
采纳的回答
Armando Longobardi
2023-6-13
Hi EK,
I'll try to give you a possible solution, based on what I have understood reading your question.
The following code does not add any new rows to the matrix. It just modify the first two columns of the matrix, so all the stimulus (second column==2) have the length of the maximum stimulus recorded.
If your desidered stimulus duration is less than the maximum one, this simple code will not be sufficient.
% read data
A = readmatrix('https://it.mathworks.com/matlabcentral/answers/uploaded_files/1409294/data%20_matrix.xlsx');
% find all start and end of the stimuli (second colum==2)
s = regexprep(num2str([0 A(:,2)'==2 0]),' ','');
StimuliIdx = [regexp(s,'01')' regexp(s,'10')'];
% compute their duration
Durations = diff(StimuliIdx,1,2);
% define desired duration
DesiredDuration = max(Durations);
% modify the first two columns so each stimulus have same length (greater
% or equal than previous one)
for iS=1:size(StimuliIdx,1)
A(StimuliIdx(iS):StimuliIdx(iS)+DesiredDuration-1,1) = A(StimuliIdx(iS),1);
A(StimuliIdx(iS):StimuliIdx(iS)+DesiredDuration-1,2) = A(StimuliIdx(iS),2);
end
% writematrix(A,'data_matrix_modified.xlsx');
I hope you find this helpful.
Best regards,
Armando
2 个评论
Armando Longobardi
2023-6-13
PS if you need to replicate also the third and fourth, consider adding them in the same for cycle.
for iS=1:size(StimuliIdx,1)
A(StimuliIdx(iS):StimuliIdx(iS)+DesiredDuration-1,1) = A(StimuliIdx(iS),1);
A(StimuliIdx(iS):StimuliIdx(iS)+DesiredDuration-1,2) = A(StimuliIdx(iS),2);
A(StimuliIdx(iS):StimuliIdx(iS)+DesiredDuration-1,3) = A(StimuliIdx(iS),3);
A(StimuliIdx(iS):StimuliIdx(iS)+DesiredDuration-1,4) = A(StimuliIdx(iS),4);
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!