# eding numbers in the matrix

1 view (last 30 days)
EK on 12 Jun 2023
Commented: EK on 13 Jun 2023
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?
John D'Errico on 12 Jun 2023
Use a loop.

Armando Longobardi on 13 Jun 2023
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.
% 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
Armando Longobardi on 13 Jun 2023
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
EK on 13 Jun 2023
Hi Armando, thank you so much! it works!

### Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!