eding numbers in the matrix

1 vue (au cours des 30 derniers jours)
EK
EK le 12 Juin 2023
Commenté : EK le 13 Juin 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?
  1 commentaire
John D'Errico
John D'Errico le 12 Juin 2023
Read in the file.
Use a loop.

Connectez-vous pour commenter.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by