fill and replicate numbers in the matrix

2 次查看(过去 30 天)
Hi,
I am trying to refill excell matrix with numbers and would like to ask if there are easy way to do it in matlab
I have matrices of 4 columns that log stimuli representation in time (logfile, 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 stimulus presentation (0=no stimulus, 2=stimulus) column 4 logs Id of repetition for 6 stimuli.
I need to mark in column2 pre stimulus interval =1 and poststimulus interval=3. For that I need to replace zeros with 1 before stimulus presentation (row 1 to 95 and then devide and replace first half of the rows after the stimulus with 3 (post stimulus interval) and another half with 1 ( pre stimulus interval) as in an example file attached below. in the column 3 I need to add Ids of repeats of each trial write it in each row (pre stimulus + stimulus+ poststimulus) as in example file. Could anyone help with this?

采纳的回答

chicken vector
chicken vector 2023-4-23
编辑:chicken vector 2023-4-23
You can re-adapt your data to exploit this very nice method to divide stimuli into a cell array.
Then is just a metter of manipulation and rewrite on the excel file.
data = readmatrix("logfile-20.4.23.xlsx");
secondColumn = abs(data(:,2)' - 2) / 2;
% Method to extract indces of consecutive stimuli [credits @KSSV]:
ad0 = zeros(size(secondColumn)) ;
ad0(logical(secondColumn)) = find(secondColumn) ;
ii = zeros(size(ad0));
jj = ad0 > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
indeces = accumarray( idx(jj)',ad0(jj)',[],@(x){x'});
% Change data:
for j = 1 : length(indeces)
midIdx = floor(sum(indeces{j}([1,end])/2));
data(indeces{j}(1):midIdx,2) = 3;
data(midIdx+1:indeces{j}(end),2) = 1;
end
% Save to Excel:
writematrix(data,filename);
Two remarks:
  • The intervals before the first stimulus and after the last stimulus are still divided into 1s and 3s. You can adjust that by moving those outside the loop.
  • It can happen that you have an odd number of intervals between two stimuli. In the first line inside the loop, use floor or ceil, if you want to give the extra place to a 1 or 3, respectively.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by