How to change a for loop code to run over columns instead of rows?
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi,
I have this piece of code which was used for a different project.
%% and splits them into stride windows of length 512 datapoints
num_cells = size(participant_columns);
num_columns = size(participant_columns{i},2);
block_size = 512;
participant_windows = {};
windowed_data = {};
stride = 64; %Change this to allow for another size of window, 512 must be divisible by stride
% if you change stride, also change l accordingly. l * stride = 512
% loops over the "participant_columns" cell array to split each of the 21 columns
% into smaller 512 element long columns.
% the smaller columns are then stored as matrices in cells.
% hence each row of the new cell array 'participant_H_windows' is made up of a list
% of cells, each cell containing a matrix full of smaller 512 element
% long columns (windows).
% in "participant_H_windows" each row represents one participant and each
% cell in that row represents one of the 21 columns
% there are different numbers of columns per row because each participants
% experience had a different length.
% index of 0 through 7 indicates the different stride windows I've defined
for l = 0:7
for j = 1:numel(participant_columns) % per participant
N = block_size*ceil(size(participant_columns{j},1)/block_size);
participant_columns{j,1}(end+1:N+stride*l,:) = NaN; % We add more NaNs at the end to make up for the deleted data
for k = 1:1:num_columns %for each measurement point we create a matrix of windows from 1 column of all data of that point
participant_windows{j,k} = reshape(participant_columns{j,1}(stride*l+1:end,k),block_size,[]);
end
end
% this makes a matrix of 8 cells which each contain all the participant_window data for that window
windowed_data{l+1,1} = participant_windows;
end
The for loop runs through all cells in a cell array and split the columns wihtin those cells into smaller 512-element long columns.
Originally "participant columns" was a 19x1 rows (see attachment "participant_columns_old" for a short version example).
participant_columns_old =
2×1 cell array
{24064×2 double}
{22016×2 double}
Now it is 1x19 columns (see attachment "participant_columns_new" for a short version example).
participant_columns_new =
1×2 cell array
{24064×1 double} {21599×1 double}
How do I need to rewrite the for loop to have it run over a list of columns instead of a list of rows?
Thank you!
댓글 수: 0
채택된 답변
Voss
2022년 2월 13일
편집: Voss
2022년 2월 13일
Seems like you can, with a couple of pretty minor modifications, get the code to work for either case (i.e., when participant_columns is a column cell array or when participant_columns is a row cell array), by indexing participant_columns with a linear index (e.g., participant_columns{j}) rather than row/column subscript index (e.g., participant_columns{j,1}):
%% and splits them into stride windows of length 512 datapoints
% num_cells = size(participant_columns);
num_cells = numel(participant_columns);
num_columns = size(participant_columns{i},2);
block_size = 512;
participant_windows = {};
windowed_data = {};
stride = 64; %Change this to allow for another size of window, 512 must be divisible by stride
% if you change stride, also change l accordingly. l * stride = 512
% loops over the "participant_columns" cell array to split each of the 21 columns
% into smaller 512 element long columns.
% the smaller columns are then stored as matrices in cells.
% hence each row of the new cell array 'participant_H_windows' is made up of a list
% of cells, each cell containing a matrix full of smaller 512 element
% long columns (windows).
% in "participant_H_windows" each row represents one participant and each
% cell in that row represents one of the 21 columns
% there are different numbers of columns per row because each participants
% experience had a different length.
% index of 0 through 7 indicates the different stride windows I've defined
for l = 0:7
for j = 1:numel(participant_columns) % per participant
N = block_size*ceil(size(participant_columns{j},1)/block_size);
participant_columns{j}(end+1:N+stride*l,:) = NaN; % We add more NaNs at the end to make up for the deleted data
for k = 1:1:num_columns %for each measurement point we create a matrix of windows from 1 column of all data of that point
participant_windows{j,k} = reshape(participant_columns{j}(stride*l+1:end,k),block_size,[]);
end
end
% this makes a matrix of 8 cells which each contain all the participant_window data for that window
windowed_data{l+1,1} = participant_windows;
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!