Replacing numbers in the matrix

5 visualizaciones (últimos 30 días)
EK
EK el 21 de En. de 2024
Comentada: Voss el 22 de En. de 2024
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 or removing stimuli ids at the end of the stimulus without adding any new rows to the matrix. For example, make all stimuli to given desiered duration 40 rows by edding or removing missing or additional stimulus ids at the end of the stimulus in colomn 1 and column 2 Could anyone help with this?
  2 comentarios
Matt J
Matt J el 21 de En. de 2024
Editada: Matt J el 21 de En. de 2024
I need make all stimuli the same length by adding or removing stimuli ids at the end of the stimulus without adding any new rows to the matrix.
Does that mean the number of rows has to remain the same as the original number of rows? That is impossible, because the number of rows in the given .xlsx file is the prime number 9371. There is no way, therefore, that the number of rows can be decomposed into an even multiple of a common stimuli duration.
EK
EK el 21 de En. de 2024
yes, the number of the rows should reain the same. I just need to replace redundant Ids numbers with 0 in column1 or with 3 in column2 to reach desireble stimulus length

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 21 de En. de 2024
"I just need to replace redundant Ids numbers with 0 in column1 or with 3 in column2 to reach desireble stimulus length"
filename = 'data _matrix (1).xlsx';
% read the data
data = readmatrix(filename);
% find where stimuli start and end
d = diff(data(:,1));
stimulus_start_idx = find(d > 0)+1;
stimulus_end_idx = find(d < 0);
% length (number of rows of data) of each stimulus
stimulus_length = stimulus_end_idx-stimulus_start_idx+1;
% number of stimuli
N_stimulus = numel(stimulus_length);
% number of rows to alter from each stimulus
n_rows_to_alter = stimulus_length-min(stimulus_length);
% prepare to mark some rows for alteration; initially none are marked
alter_row = false(size(data,1),1);
% loop over all stimuli
for ii = 1:N_stimulus
% for stimulus ii, mark the last n_rows_to_alter(ii) rows for alteration
alter_row(stimulus_end_idx(ii)+(-n_rows_to_alter(ii)+1:0)) = true;
end
% alter the rows
data(alter_row,1) = 0;
data(alter_row,2) = 3;
  4 comentarios
EK
EK el 22 de En. de 2024
Thank you so much! You are amazing!
Voss
Voss el 22 de En. de 2024
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by