eding numbers in the matrix

2 ビュー (過去 30 日間)
EK
EK 2023 年 6 月 12 日
コメント済み: EK 2023 年 6 月 13 日
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 件のコメント
John D'Errico
John D'Errico 2023 年 6 月 12 日
Read in the file.
Use a loop.

サインインしてコメントする。

採用された回答

Armando Longobardi
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
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
EK
EK 2023 年 6 月 13 日
Hi Armando, thank you so much! it works!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by